dictionary as attribute of a class...
tinauser
tinauser at libero.it
Wed Jan 5 10:07:58 EST 2011
Hallo list,
here again I have a problem whose solution might be very obvious, but
I really cannot see it:
I have a class having as attribute a dictionary whose keys are names
and values are instance of another class.
This second class has in turn as an attribute a dictionary.
I want a function of the first class that can change value of one of
the second class instance's dictionary.
however I cannot do it without modifying this attribute for ALL the
instance of the second class contained in the first class' dictionary.
What I'm doing wrong?
the code:
###############
###can i change a dictionary attribute of an instantated object
without affectin all the instances of that object?
class mistClass():
def __init__(self,name,cDict={}):
print 'mistClass ',name,' Init'
self._name=name
self._cDict=cDict
def setName(self,n):
self._name=n
def getName(self):
return self._name
## def setDict(self,one,two):
## self._cDict['one']=one
## self._cDict['two']=two
def setDict(self,listK,listV):
assert len(listK)==len(listV)
for k,v in zip(listK,listV):
self._cDict[k]=v
def getDict(self):
return self._cDict
class mistClassContainer():
def __init__(self,name,dict_of_mistclass={}):
print 'init mistClassContainer ',name
self._name=name
self._DOM=dict_of_mistclass
def add_mistclass(self,mc):
for el in mc:
self._DOM[el]=mistClass(el)
## def mod_mistclasscDict(self,mc,one,two):
## self._DOM[mc].setDict(one,two)
def mod_mistclasscDict(self,mc,lK,lV):
self._DOM[mc].setDict(lK,lV)
a=mistClassContainer('firsmistclasscontainer')
a.add_mistclass(['mc1','mc2','mc3','mc4','mc5','mc6'])
print 'before modification'
for el in a._DOM.iterkeys():
print a._DOM[el].getDict()
print a._DOM[el].getName()
a.mod_mistclasscDict('mc1',['one','two'],['modone','modtwo'])
print 'after modification'
for el in a._DOM.iterkeys():
print a._DOM[el].getDict()
print a._DOM[el].getName()
b=mistClass('mc7')
print b.getDict()
print b.getName()
b.setName('modified name')
b.getName()
for el in a._DOM.iterkeys():
print a._DOM[el].getName()
More information about the Python-list
mailing list