Short answer:
Either define a __len__
method or check whether 'some_other_class'
is present in self.__dict__.keys()
.
Long answer:
The recursion occurs when
- you try to get a non-existent attribute from a class and
- you have a
__getattr__
method in that class and
- within the
__getattr__
method you try to get a non-existent attribute from the same class
The recursion error can for example be obtained in the following example, where self.some_other_class
is not defined:
class Test1:
def __init__(self):
self.arg1 = 'abc'
self.arg2 = 'def'
return
def __getattr__(self, item):
print(item)
self.some_other_class
return
x = Test1()
x.test
When x.test is called, the __getattr__
method is called, which calls self.some_other_class
, which again calls the __getattr__
method, which again calls self.some_other_class
, etc.
Of course in your case the self.some_other_class
is defined, and then there is no recursion:
class SomeOtherClass:
def __init__(self):
pass
class Test1:
def __init__(self):
self.arg1 = 'abc'
self.arg2 = 'def'
self.some_other_class = SomeOtherClass
return
def __getattr__(self, item):
print(item)
self.some_other_class
return
x = Test1()
x.test
However, in the debug mode of PyCharm probably the __len__
attribute of the class is called after manually running a line in the file. In the example this means that after the first two lines in the Test1.__init__
method self.some_other_class
is not defined yet, and also Test1.__len__
is not defined. Therefore calling the __len__
attribute will result in the recursion error.
There are two possible solutions:
Define the __len__
method:
class Test1:
...
def __len__(self):
return 1
Check in __getattr__
whether the passed item is present in the class:
class Test1:
...
def __getattr__(self, item):
print(item)
if 'some_other_class' in self.__dict__:
if hasattr(self.some_other_class, 'blabla')):
abc = 123
return
Note that just calling getattr(self.some_other_class, 'blabla')
or hasattr(self.some_other_class, 'blabla')
without the check in self.__dict__
also triggers the __getattr__
method.