Storing Instances of Objects in a Nested Dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nkyaelly
    New Member
    • Nov 2007
    • 2

    Storing Instances of Objects in a Nested Dictionary

    Hi People,

    I am getting frustrated with the way Python handles its dictionary.

    I have a scenarion where a class A is stored in a dictionary. The class A contains a dictionary B as a field as below

    Code:
    class B:
        bName = ""
    
    class A:
        aName = ""
        b = {}
    
        def AddB(self, bKey, bVal):
            newB = B()
            newB.bName = bVal
            self.b[bKey] = newB
        
    aList = {}
    
    
    #Now i attempt to add an instance of A into the dictionary the instance #containing a element in B as below.
    
    a1 = A()
    a1.name = "first"
    a1.AddB("firstValKey", "firstVal")
    aList["firstKey"] = a1
    
    # I add another instance of A into the the dictionary. Notice hear no element in # B is set
    a2 = A()
    a2.name = "second"
    aList["secondKey"] = a2
    
    # Then i print out the contents
    for aKey, aValue in aList.iteritems():
        print aKey + "_" + aValue.aName
        for bKey, bValue in aValue.b.iteritems():
          print bKey + "_" + bValue.bName
        print "NEXT"
    # ERROR: I get this result.
    secondKey_
    _firstValKey_fi rstVal
    NEXT
    firstKey_
    _firstValKey_fi rstVal
    NEXT

    #EXPECTED RESULT
    secondKey_
    NEXT
    firstKey_
    _firstValKey_fi rstVal
    NEXT


    Question: Can you help me get the desired result. Would be very much appreciated
    Last edited by nkyaelly; Nov 28 '07, 08:25 PM. Reason: wording
  • KaezarRex
    New Member
    • Sep 2007
    • 52

    #2
    Originally posted by nkyaelly
    Hi People,

    I am getting frustrated with the way Python handles its dictionary.

    I have a scenarion where a class A is stored in a dictionary. The class A contains a dictionary B as a field as below

    Code:
    class B:
        bName = ""
    
    class A:
        aName = ""
        b = {}
    
        def AddB(self, bKey, bVal):
            newB = B()
            newB.bName = bVal
            self.b[bKey] = newB
        
    aList = {}
    
    
    #Now i attempt to add an instance of A into the dictionary the instance #containing a element in B as below.
    
    a1 = A()
    a1.name = "first"
    a1.AddB("firstValKey", "firstVal")
    aList["firstKey"] = a1
    
    # I add another instance of A into the the dictionary. Notice hear no element in # B is set
    a2 = A()
    a2.name = "second"
    aList["secondKey"] = a2
    
    # Then i print out the contents
    for aKey, aValue in aList.iteritems():
        print aKey + "_" + aValue.aName
        for bKey, bValue in aValue.b.iteritems():
          print bKey + "_" + bValue.bName
        print "NEXT"
    # ERROR: I get this result.
    secondKey_
    _firstValKey_fi rstVal
    NEXT
    firstKey_
    _firstValKey_fi rstVal
    NEXT

    #EXPECTED RESULT
    secondKey_
    NEXT
    firstKey_
    _firstValKey_fi rstVal
    NEXT


    Question: Can you help me get the desired result. Would be very much appreciated
    It looks like the dictionary "b" in class "A" is shared with every instance of class "A". I don't know the exact reason for this, but it might have something to do with the items in the dictionary being stored by reference. To fix your problem, modify class "A".
    [CODE=python]class A:

    aName = ""

    def __init__(self):
    self.b = {}

    def AddB(self, bKey, bVal):
    newB = B()
    newB.bName = bVal
    self.b[bKey] = newB[/CODE]

    Comment

    • nkyaelly
      New Member
      • Nov 2007
      • 2

      #3
      It worked like a charm. Many thanks ++

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by nkyaelly
        Hi People,

        I am getting frustrated with the way Python handles its dictionary.

        I have a scenarion where a class A is stored in a dictionary. The class A contains a dictionary B as a field as below

        Code:
        class B:
            bName = ""
        
        class A:
            aName = ""
            b = {}
        
            def AddB(self, bKey, bVal):
                newB = B()
                newB.bName = bVal
                self.b[bKey] = newB
            
        aList = {}
        
        
        #Now i attempt to add an instance of A into the dictionary the instance #containing a element in B as below.
        
        a1 = A()
        a1.name = "first"
        a1.AddB("firstValKey", "firstVal")
        aList["firstKey"] = a1
        
        # I add another instance of A into the the dictionary. Notice hear no element in # B is set
        a2 = A()
        a2.name = "second"
        aList["secondKey"] = a2
        
        # Then i print out the contents
        for aKey, aValue in aList.iteritems():
            print aKey + "_" + aValue.aName
            for bKey, bValue in aValue.b.iteritems():
              print bKey + "_" + bValue.bName
            print "NEXT"
        # ERROR: I get this result.
        secondKey_
        _firstValKey_fi rstVal
        NEXT
        firstKey_
        _firstValKey_fi rstVal
        NEXT

        #EXPECTED RESULT
        secondKey_
        NEXT
        firstKey_
        _firstValKey_fi rstVal
        NEXT


        Question: Can you help me get the desired result. Would be very much appreciated
        Dictionary 'b' is a class variable. Instead of[code=Python]self.b[bKey] = newB[/code]it would be proper to do this:[code=Python]A.b[bKey] = newB[/code]Class variables are shared among all instances of that class. By creating dictionary 'b' in A.__init__() as KaezarRex suggested, it is an instance variable.

        Comment

        Working...