I'm trying to call the instance method of objects inside a dictionary who have the objects themselves, all are from the same class. But i have a infinite recursion, i think that 'self' doesn't change and reference the first object all the time.
class Foo():
name = 'name'
foo_objects = {}
def print_name(self):
output = self.name
for key, value in self.foo_objects.items():
output += value.print_name()
return output
I'm using it like this:
def main():
foo = Foo()
foo.foo_objects['key'] = Foo()
print foo.print_name()
if __name__ == '__main__':
sys.exit(main())
And i get this error:
...etc...
output += value.print_name()
File "C:\dev\python\projects\test\test.py", line 41, in print_name
output += value.print_name()
File "C:\dev\python\projects\test\test.py", line 41, in print_name
output += value.print_name()
RuntimeError: maximum recursion depth exceeded
What´s wrong? What I'm not understanding of python recursion? How can be do? Thanks for any help, and excuse me my bad english.