Hi there,
once again my poor basic knowledge force me in some probably quite stupid
error.
I have a class1 that has as attribute a dictionari D1.
I have a class2 that has as attribute a dictionary D2 whose keys are string
and values are instantiation of class1.
I want a method of class2 that can modify the dictionary D1 of one of the
class1 that are presente in D2.
In the code you can find attached, there is my try. I cannot modify the D1 of
one instantiation of class1, without modifying all the instances.
What am I doing wrong?
Thanks a lot
problemwithdict.py (1.59 KB)
This question does not belong on this list. It is not related to wx,
or wxPython. This is a general python question and should be posed on
a list dedicated to that purpose.
That said, your problem is:
···
--
class mistClass():
def __init__(self,name,cDict={}):
print 'mistClass ',name,' Init'
self._name=name
self._cDict=cDict
--
The cDict={} is interpreted once, when the class is defined. This
means each instance is using the SAME DICT.
You need something like:
--
class mistClass():
def __init__(self,name,cDict=None):
print 'mistClass ',name,' Init'
self._name=name
self._cDict=cDict or {}
--
So that the dictionary is created when __init__() is called, not
defined, such that each instance has it own dict.
Thanks for the reply.
You are right, the topic does not belong to this list, I posted it
also here:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/02abb6f2ee78a097#
Thanks anyway for the answer!
···
On Jan 5, 4:22 pm, Ben Timby <bti...@gmail.com> wrote:
This question does not belong on this list. It is not related to wx,
or wxPython. This is a general python question and should be posed on
a list dedicated to that purpose.
That said, your problem is:
--
class mistClass():
def __init__(self,name,cDict={}):
print 'mistClass ',name,' Init'
self._name=name
self._cDict=cDict
--
The cDict={} is interpreted once, when the class is defined. This
means each instance is using the SAME DICT.
You need something like:
--
class mistClass():
def __init__(self,name,cDict=None):
print 'mistClass ',name,' Init'
self._name=name
self._cDict=cDict or {}
--
So that the dictionary is created when __init__() is called, not
defined, such that each instance has it own dict.