Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38114

Re: __getattr__ Confusion

From Terry Reedy <tjreedy@udel.edu>
Subject Re: __getattr__ Confusion
Date 2013-02-03 21:47 -0500
References <095a0432-f8a4-40b4-96ed-1588896fba66@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1320.1359946086.2939.python-list@python.org> (permalink)

Show all headers | View raw


On 2/3/2013 8:08 PM, Saul Spatz wrote:
> To the good people on comp.lang.python:
>
> I have the following Tkinter class (python 2.7.3):
>
> from Tkinter import *
>
> class ScrolledCanvas(Frame): def __init__(self, master, width,
> height, bg, cursor): Frame.__init__(self, master) self.__nonzero__ =
> lambda: True canv = self.canvas = Canvas(self, bg=bg, relief=SUNKEN)
> # self.__getattr__ = lambda x, name: getattr(self.canvas, name)
> canv.config(width=width, height=height)           # display area
> size canv.config(scrollregion=(0, 0, width, height))   # canvas size
> corners canv.config(highlightthickness=0)                 # no pixels
> to border
>
> ybar = Scrollbar(self) ybar.config(command=canv.yview)
> # xlink sbar and canv canv.config(yscrollcommand=ybar.set)
> # move one moves other
>
> xbar = Scrollbar(self) xbar.config(command=canv.xview)
> # xlink sbar and canv canv.config(xscrollcommand=xbar.set)
> # move one moves other
>
> canv.grid(row = 0, column = 0, sticky = 'news') ybar.grid(row = 0,
> column = 1, sticky = 'ns') xbar.grid(row = 1, column = 0, sticky =
> 'ew') self.rowconfigure(0, weight = 1) self.columnconfigure(0, weight
> = 1)
>
> self.create_text(20, 20, text = 'Did it!', fill = 'red')
>
> def __getattr__(self, name): return getattr(self.canvas, name)
>
> root = Tk() app = ScrolledCanvas(root, 400, 300, 'white', 'hand2')
> app.pack() root.mainloop()
>
> I just added the __getattr__ method, and the program crashed in the
> Canvas constructor.  There is apparently a call to self.__nonzero__
> somewhere in Tkinter.py, and since the constructor hasn't exited yet,
> sel.fcanvas isn't defined yet, so __getattr__ recurses indefinitely.
>
> I fixed this as you see, by defining self.__nonzero__ before the call
> to the constructor.  Now, I have  two questions:
>
> 1. I originally defined self.__nonzero__ = lambda x: True, on the
> assumption that when self.__nonzero__ was called, the interpreter
> would pass self as an argument.  Wrong.  No arguments were passed.
> Why is this?

Because you made __nonzero__ an instance function attribute instead of 
an instance method class attribute as would be the case if you wrote

def __nonzero__(self): return True

outside of __init__.
>
> 2. I find this solution rather unsatisfactory, since there's a rather
> obscure line of code here.  I tried eliminating the def of
> __gertattr__ and the definition of self.__nonzero__ and adding this
> line after the constructor:
>
> self.__getattr__=  lambda name: getattr(self.canvas, name)

I presume __getattr__ is only looked up on the class and never on the 
instance, not even as a backup.

> This get through the constructor all right, but crashes with the
> message that a ScrolledCanvas object has no create_text attribute.
> (I've tried passing two arguments to the lambda, but it makes no
> difference.)

If you are just starting out, consider 3.3 unless you really have to use 
2.7.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

__getattr__ Confusion Saul Spatz <saul.spatz@gmail.com> - 2013-02-03 17:08 -0800
  Re: __getattr__ Confusion Chris Angelico <rosuav@gmail.com> - 2013-02-04 12:28 +1100
  Re: __getattr__ Confusion Terry Reedy <tjreedy@udel.edu> - 2013-02-03 21:47 -0500
  Re: __getattr__ Confusion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-04 04:35 +0000
    Re: __getattr__ Confusion Saul Spatz <saul.spatz@gmail.com> - 2013-02-04 05:44 -0800
      Re: __getattr__ Confusion Peter Otten <__peter__@web.de> - 2013-02-04 15:15 +0100
        Re: __getattr__ Confusion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 01:46 +1100
        Re: __getattr__ Confusion Saul Spatz <saul.spatz@gmail.com> - 2013-02-04 09:29 -0800
          Re: __getattr__ Confusion Peter Otten <__peter__@web.de> - 2013-02-04 19:23 +0100
          Re: __getattr__ Confusion Chris Angelico <rosuav@gmail.com> - 2013-02-05 12:00 +1100
        Re: __getattr__ Confusion Saul Spatz <saul.spatz@gmail.com> - 2013-02-04 09:29 -0800

csiph-web