I have a class that looks somthing like this:
class A(dict):
info = {'num': 0}
def __init__(self, **kwargs):
self.__dict__.setdefault('info', self.info)
When I'm creating instances of this class they referenced to the same object:
>>> a = A()
>>> b = A()
>>>
>>> b.info['num']
0
>>> a.info['num'] = 1
>>> b.info['num']
1
>>>
I'm clearly doing something wrong.
Problems:
- Instances of
a
andb
are referenced to the same object.
Desired output:
>>> a = A()
>>> b = A()
>>>
>>> b.info['num']
0
>>> a.info['num'] = 1
>>> b.info['num']
0
>>>
Question:
- How to change the class in order to keep this "template" of
info
? - Why the current approuch doesn't work?