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
# 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
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"
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
Comment