maximum recursion depth is reducing?

Jp Calderone exarkun at intarweb.us
Tue Feb 25 13:53:12 EST 2003


On Tue, Feb 25, 2003 at 06:18:32PM +0000, Xiao-Qin Xia wrote:
> Dear Steven Taschuk, 
> 
> >     __getattr__(self, name)
> >         Called when an attribute lookup has not found the attribute in the
> >         usual places (i.e. it is not an instance attribute nor is it found
> >         in the class tree for self). [...]
> > 
> > So if self.text_widget has been bound, a reference to it will be
> > resolved normally, and __getattr__ will not be called.
>  
> I hope so, but it doesn't seem to be the truth. In the codes I presented, 
> self.text_widget should be created at the same time when the instace is 
> created. But obviously __getattr__ will be called if __init__ calls Text to 
> create self.text_widget, instead of create it by itself. 

  Not quite.  Everything in __init__ does not occur simultaneously.  Each
line is executed before the line following it.  In the original code,

         def __init__(self, master):
                 Frame.__init__(self,master)
                 self.master = master
                 self.pack()
                 self.Text(self)


  First self.master is assigned, then self.pack is looked up.  It isn't
found, because it doesn't exist, so __getattr__ is called with 'pack' as the
name.  i is incremented, getattr(self.text_widget, 'pack') is called. 
self.text_widget is not found!  So __getattr__ is called with 'text_width'
as the name.  Repeat, ad infinitum.

> 
> This problem make it to be dangerous to override __getattr__.
> 

  Carelessly written __getattr__ functions can indeed lay waste to whole
contintents.  Take care when writting them.

  Jp

-- 
There are 10 kinds of people: those who understand binary and those who do
not.
-- 
 up 16 days, 22:29, 4 users, load average: 0.07, 0.05, 0.01
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030225/d2423b7d/attachment.sig>


More information about the Python-list mailing list