define

Andrew Bennetts andrew-pythonlist at puzzling.org
Mon May 12 23:36:08 EDT 2003


On Mon, May 12, 2003 at 05:44:54PM +0100, Turhan Ozen wrote:
>    __getattr__ is getting into infinite loop. I just copied the code. Could
>    you please help me how to avoid this?

The problem is that __init__'s does "self.__names = {}", which invokes
__setattr__, which invokes __getattr__ recursively...

Once that line is executed, it should work okay, so change the __setattr__
to be:

       def __setattr__(self, name, value):
           # Catch the initial assignment of __names
           if name == '_' + self.__class__.__name__ + '__names':
               object.__setattr__(self, name, value)
               return

           try: 
               self[self.__names[name]] = value
           except LookupError: 
               object.__setattr__(self, name, value)


(Untested)

-Andrew.






More information about the Python-list mailing list