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


Groups > comp.lang.python > #38114

Re: __getattr__ Confusion

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.04; 'root': 0.04; 'true,': 0.04; '(python': 0.05; 'attribute': 0.05; 'arguments': 0.07; 'canvas': 0.07; 'column': 0.07; 'constructor': 0.07; 'method,': 0.07; 'tkinter': 0.07; 'attribute.': 0.09; 'exited': 0.09; 'lambda:': 0.09; 'name)': 0.09; 'name):': 0.09; 'pixels': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'looked': 0.10; 'def': 0.10; "hasn't": 0.15; 'passing': 0.15; 'argument.': 0.16; 'called,': 0.16; 'constructor.': 0.16; 'crashed': 0.16; 'crashes': 0.16; 'eliminating': 0.16; 'height,': 0.16; 'lambda': 0.16; 'passed.': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'tk()': 0.16; 'width,': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'instance,': 0.17; 'jan': 0.18; 'app': 0.19; 'import': 0.21; 'questions:': 0.22; 'defined': 0.22; 'originally': 0.23; 'somewhere': 0.24; 'pass': 0.25; 'tried': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wrote': 0.26; 'see,': 0.27; 'fixed': 0.28; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; 'obscure': 0.29; 'definition': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'code': 0.31; 'defining': 0.33; 'to:addr:python-list': 0.33; 'text': 0.34; 'self': 0.34; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'display': 0.36; 'two': 0.37; 'why': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; '20,': 0.65; 'border': 0.65; '2.7.': 0.84; 'moves': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: __getattr__ Confusion
Date Sun, 03 Feb 2013 21:47:37 -0500
References <095a0432-f8a4-40b4-96ed-1588896fba66@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <095a0432-f8a4-40b4-96ed-1588896fba66@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1320.1359946086.2939.python-list@python.org> (permalink)
Lines 77
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359946086 news.xs4all.nl 6875 [2001:888:2000:d::a6]:52069
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38114

Show key headers only | 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