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


Groups > comp.lang.python > #38123

Re: __getattr__ Confusion

From Peter Otten <__peter__@web.de>
Subject Re: __getattr__ Confusion
Date 2013-02-04 15:15 +0100
Organization None
References <095a0432-f8a4-40b4-96ed-1588896fba66@googlegroups.com> <510f3a91$0$29866$c3e8da3$5496439d@news.astraweb.com> <863c89b0-2125-4525-8ad8-1111b0a6beae@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1322.1359987291.2939.python-list@python.org> (permalink)

Show all headers | View raw


Saul Spatz wrote:

> Now I have another question.  If dunder methods are looked up only in the
> class, not the instance, why did defining __nonzero__ the way I did work? 
> Shouldn't I have had to define it with a def?  Is __nonzero__ a special
> case?

Unfortunately the situation is a bit more complex. Classic classes (like 
Tkinter.Frame) behave differently from newstyle classes (subclasses of 
object):

>>> def nz():
...     print "nonzero"
...     return 0
... 
>>> class Classic: pass
... 
>>> c = Classic()
>>> c.__nonzero__ = nz
>>> not c
nonzero
True
>>> class New(object): pass
... 
>>> n = New()
>>> n.__nonzero__ = nz
>>> not n
False

So Steven is wrong here.

> Shouldn't I have had to define it with a def?

If you mean as opposed to a lambda, there is no difference between

f = lambda ...

and

def f(...): ...

other than that the last one gives you a nice name in a traceback.

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