maximum recursion depth is reducing?

Xiao-Qin Xia xx758 at cam.ac.uk
Tue Feb 25 06:34:02 EST 2003


Hi, everybody,

The following codes can run with Python1.5 to Python2.2, but cannot run 
with Python2.3a.

############################
from Tkinter import *

class MessageWindow(Frame):
        #text_widget = None
        def __init__(self, master):
                Frame.__init__(self,master)
                self.master = master
                self.pack()
                self.Text(self)

        def Text(self, master):
                self.text_widget = Text(master, bg="white")
                self.text_widget.pack()
                
        i = 0
        def __getattr__(self, attr_name):
                print self.i
                self.i = self.i + 1
                return getattr(self.text_widget, attr_name)
                
if __name__ == "__main__":
        r = Tk()
        select  = MessageWindow(r)
        r.mainloop()
###############################

****Question 1****:

With Python1.5, it will print number from 0 to 19987, and then display a 
window,
With Python2.1 and Python2.2 it will print number from 0 to 1987, and then 
display a window,
With Python2.3a, it print many lines like:
"""
  ...... 
  File "tt.py", line 19, in __getattr__
    return getattr(self.text_widget, attr_name)
  File "tt.py", line 19, in __getattr__
    return getattr(self.text_widget, attr_name)
  File "tt.py", line 19, in __getattr__
    return getattr(self.text_widget, attr_name)
RuntimeError: maximum recursion depth exceeded
"""

Does that mean the maximum recursion depth is reducing from 19987 
(Python1.5.2) to 1987(Python2.1-2.2) to less (Python2.3a)?

****Question 2****:

1. If enable (uncomment) the line below the class definition line:
text_widget = None

All versions of python will print 0 and 1, and then display a window.

or 
2. If use the codes of method Text to replace the call to Text in __init__, 
i.e. __init__ become:
        def __init__(self, master):
                Frame.__init__(self,master)
                self.master = master
                self.pack()
                self.text_widget = Text(master, bg="white")
                self.text_widget.pack()

All versions of python will disply a window without printing anything.

Does this mean Python will allways call __getattr__ when call a method, but 
not call __getattr__ when citing a local attribute variable?

Best regards,

York Xia





More information about the Python-list mailing list