7

for example i use this code:

class A(object):
    def __init__(self):
        self.dict1 = {
            'A': 3,
            'B': self.A}
    def __getattr__(self, key):
        if key in self.dict1:
            return self.dict1[key]
a = A()

and when it's runned it throws maximum recursion depth exceeded. Can someone please tell me what am i doing wrong here

M1k3
  • 79
  • 1
  • 3

1 Answers1

12

The reference to self.dict1 inside your __getattr__ method causes __getattr__ to be called again, and so on, hence the infinite recursion. The only safe way to access attributes of self inside __getattr__ is by using references to self.__dict__. Try

def __getattr__(self, key):
    if key in self.__dict__['dict1']:
        return self.__dict__['dict1'][key]

Note also that the absence of an else clause will mean undefined attributes appear to have the value None.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • 1
    Tried you solution and it raise KeyError instead – M1k3 Jan 16 '16 at 16:51
  • That's because in trying to construct `self.dict1` in the `__init__` method you attempt tp access `self.A`. This triggers a call to `__getattr__`, which assumes the existence of `self.dict1`. – holdenweb Jan 16 '16 at 17:38
  • Maybe not very ellegant, but it works if i just set 'B' after dict1 is created like this: `self.dict1['B'] = self.A` – M1k3 Jan 16 '16 at 18:09
  • As long as you've got something that works. Elegance can come later! – holdenweb Jan 17 '16 at 09:25
  • Don't forget as well that if you implement `__setattr__` the `__init__` method should use `self.__dict__['dict1'] = {}`. – holdenweb Sep 21 '17 at 12:33