By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

Storing Instances of Objects in a Nested Dictionary

P: 2
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

Expand|Select|Wrap|Line Numbers
  1. class B:
  2.     bName = ""
  3.  
  4. class A:
  5.     aName = ""
  6.     b = {}
  7.  
  8.     def AddB(self, bKey, bVal):
  9.         newB = B()
  10.         newB.bName = bVal
  11.         self.b[bKey] = newB
  12.  
  13. aList = {}
  14.  
  15.  
  16. #Now i attempt to add an instance of A into the dictionary the instance #containing a element in B as below.
  17.  
  18. a1 = A()
  19. a1.name = "first"
  20. a1.AddB("firstValKey", "firstVal")
  21. aList["firstKey"] = a1
  22.  
  23. # I add another instance of A into the the dictionary. Notice hear no element in # B is set
  24. a2 = A()
  25. a2.name = "second"
  26. aList["secondKey"] = a2
  27.  
  28. # Then i print out the contents
  29. for aKey, aValue in aList.iteritems():
  30.     print aKey + "_" + aValue.aName
  31.     for bKey, bValue in aValue.b.iteritems():
  32.       print bKey + "_" + bValue.bName
  33.     print "NEXT"
  34.  
  35.  
# ERROR: I get this result.
secondKey_
_firstValKey_firstVal
NEXT
firstKey_
_firstValKey_firstVal
NEXT

#EXPECTED RESULT
secondKey_
NEXT
firstKey_
_firstValKey_firstVal
NEXT


Question: Can you help me get the desired result. Would be very much appreciated
Nov 28 '07 #1
Share this Question
Share on Google+
3 Replies


KaezarRex
P: 52
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

Expand|Select|Wrap|Line Numbers
  1. class B:
  2.     bName = ""
  3.  
  4. class A:
  5.     aName = ""
  6.     b = {}
  7.  
  8.     def AddB(self, bKey, bVal):
  9.         newB = B()
  10.         newB.bName = bVal
  11.         self.b[bKey] = newB
  12.  
  13. aList = {}
  14.  
  15.  
  16. #Now i attempt to add an instance of A into the dictionary the instance #containing a element in B as below.
  17.  
  18. a1 = A()
  19. a1.name = "first"
  20. a1.AddB("firstValKey", "firstVal")
  21. aList["firstKey"] = a1
  22.  
  23. # I add another instance of A into the the dictionary. Notice hear no element in # B is set
  24. a2 = A()
  25. a2.name = "second"
  26. aList["secondKey"] = a2
  27.  
  28. # Then i print out the contents
  29. for aKey, aValue in aList.iteritems():
  30.     print aKey + "_" + aValue.aName
  31.     for bKey, bValue in aValue.b.iteritems():
  32.       print bKey + "_" + bValue.bName
  33.     print "NEXT"
  34.  
  35.  
# ERROR: I get this result.
secondKey_
_firstValKey_firstVal
NEXT
firstKey_
_firstValKey_firstVal
NEXT

#EXPECTED RESULT
secondKey_
NEXT
firstKey_
_firstValKey_firstVal
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".
Expand|Select|Wrap|Line Numbers
  1. class A:
  2.  
  3.     aName = ""
  4.  
  5.     def __init__(self):
  6.         self.b = {}
  7.  
  8.     def AddB(self, bKey, bVal):
  9.         newB = B()
  10.         newB.bName = bVal
  11.         self.b[bKey] = newB
Nov 28 '07 #2

P: 2
It worked like a charm. Many thanks ++
Nov 28 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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

Expand|Select|Wrap|Line Numbers
  1. class B:
  2.     bName = ""
  3.  
  4. class A:
  5.     aName = ""
  6.     b = {}
  7.  
  8.     def AddB(self, bKey, bVal):
  9.         newB = B()
  10.         newB.bName = bVal
  11.         self.b[bKey] = newB
  12.  
  13. aList = {}
  14.  
  15.  
  16. #Now i attempt to add an instance of A into the dictionary the instance #containing a element in B as below.
  17.  
  18. a1 = A()
  19. a1.name = "first"
  20. a1.AddB("firstValKey", "firstVal")
  21. aList["firstKey"] = a1
  22.  
  23. # I add another instance of A into the the dictionary. Notice hear no element in # B is set
  24. a2 = A()
  25. a2.name = "second"
  26. aList["secondKey"] = a2
  27.  
  28. # Then i print out the contents
  29. for aKey, aValue in aList.iteritems():
  30.     print aKey + "_" + aValue.aName
  31.     for bKey, bValue in aValue.b.iteritems():
  32.       print bKey + "_" + bValue.bName
  33.     print "NEXT"
  34.  
  35.  
# ERROR: I get this result.
secondKey_
_firstValKey_firstVal
NEXT
firstKey_
_firstValKey_firstVal
NEXT

#EXPECTED RESULT
secondKey_
NEXT
firstKey_
_firstValKey_firstVal
NEXT


Question: Can you help me get the desired result. Would be very much appreciated
Dictionary 'b' is a class variable. Instead of
Expand|Select|Wrap|Line Numbers
  1. self.b[bKey] = newB
it would be proper to do this:
Expand|Select|Wrap|Line Numbers
  1. A.b[bKey] = newB
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.
Nov 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.