I am trying to modify an attribute that is defined in a class. For this problem, I had to modify the names of the attributes to include private_ in the beginning. So, if it was previously self.a
, it is now self.private_a
in self.__dict__
. However, it should still work with the methods defined in the class. I have this bump method that increments some of the attributes, and when getattr
is implemented, the updated values are added to a new key instead.
For example, if my self.dict is
{'private_a': 1, 'private_b': 2, 'c': 3, 'd': 5}
and I call bump, I get this
{'private_a': 1, 'private_b': 2, 'c': 4, 'd': 5, 'a': 2, 'b': 3}
What I want is:
{'private_a': 2, 'private_b': 3, 'c': 4, 'd': 5 }
What am i doing wrong?
def bump(self):
self.a += 1
self.b += 1
self.c += 1
def __getattr__(self,name):
calling = inspect.stack()[1]
if calling.function in C.__dict__:
actual = 'private_' + name
if actual in self.__dict__:
return self.__dict__[actual]
if name not in self.__dict__:
raise NameError