Hi everyone,
I would like to create a class with a default dictionary attribute.
and when being Instantiated I would like to add (or not) some items to that attribute (
Any ideas ?
Thanks
I would like to create a class with a default dictionary attribute.
and when being Instantiated I would like to add (or not) some items to that attribute (
attrib
in the following example)class CommonCar: attrib = { 'wheel':4, 'automatic':False} def __init__(self, addattrib=None, value=1): self.value = value if addattrib != None: self.attrib |= addattrib #instantiation polo = CommonCar({'brand':'VW'}) #Check the items inside the class CommonCar print('CommonCar'.upper()) for k,v in CommonCar.attrib.items(): print(k, v) #Check the items inside [inline]attrib[/inline] print('\npolo'.upper()) for k,v in polo.attrib.items(): print(k, v)
Output:COMMONCAR
wheel 4
automatic False
brand VW #<-------------- (should not have this item!!)
POLO
wheel 4
automatic False
brand VW
So the output is unexpected, because the class COMMONCAR get also the merge dict. of the instantiation of polo Any ideas ?
Thanks
![[Image: NfRQr9R.jpg]](https://i.imgur.com/NfRQr9R.jpg)