471,350 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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

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
3 1613
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
It worked like a charm. Many thanks ++
Nov 28 '07 #3
bvdet
2,851 Expert Mod 2GB
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.

Similar topics

7 posts views Thread by Kerry Neilson | last post: by
reply views Thread by Calvin Spealman | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.