__getattr__ possible loop

Maksim Kasimov maksim.kasimov at gmail.com
Thu Dec 28 10:24:23 EST 2006


bearophileHUGS at lycos.com wrote:
> I have tried this, with Psyco it segfaults, and with Python 2.5 (on
> Win) hangs the interpreter, is it possible to improve the situation?
> 
> class T(object):
>   def __getattr__(self, x): dir(self)
> #import psyco
> #psyco.full()
> T().method()
> 
> (Probably dir calls __getattr__).
> 
> Bye,
> bearophile
> 

how to improve the situation depends on what do you expect to get by calling "T().method()"

dir calls __getattr__ with the value '__members__', for example you can write:

     def __getattr__(self, x):

             if x == '__members__':
                 return ('method1', 'field1', )

-- 
Maksim Kasimov



More information about the Python-list mailing list