Method calls and stack consumption

Peter Otten __peter__ at web.de
Sun Apr 15 01:27:25 EDT 2007


Martin Manns wrote:

> Calling methods of other object instances seems quite expensive on the
> stack (see example below). Is there a better way of traversing through
> methods of instances that are connected in a cyclic graph? (The real
> program's graph contains multiple successors in lists.)
> 
> class A(object):
>     def __init__(self):
>         self.i = 0
>     def a(self):
>         if self.i % 1000 == 0:
>             print self.i
>         self.i += 1                     
          return S[self].a
> 
> a = A()
> b = A()
> S = {a:b, b:a}

a = a.a
while True:
    a = a()

That's how you can do it if your real program is similar enough to the
example...

Peter




More information about the Python-list mailing list