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 -
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_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
3 1829
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 -
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_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". - class A:
-
-
aName = ""
-
-
def __init__(self):
-
self.b = {}
-
-
def AddB(self, bKey, bVal):
-
newB = B()
-
newB.bName = bVal
-
self.b[bKey] = newB
It worked like a charm. Many thanks ++
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 -
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_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
it would be proper to do this:
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kerry Neilson |
last post by:
Hi,
Really hung up on this one. I'm trying to get all the fields of a
dictionary to be unique for each class:
class A {
my_dict =
dict_entry = { 'key1':0, 'key2':0 }
__init__(self):
for...
|
by: Bugs |
last post by:
Does anyone have any recommendations on the best way to create multiple
instances of the same class when the final number of instances is unknown?
For example, I have a class and based on some...
|
by: mk |
last post by:
Hello everyone,
I'm storing functions in a dictionary (this is basically for cooking up
my own fancy schmancy callback scheme, mainly for learning purpose):
.... return "f2 " + arg
.......
|
by: Calvin Spealman |
last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote:
As was pointed out already, this is a basic misunderstanding of
assignment, which is common with people learning Python.
To your...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |