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


Groups > comp.lang.python > #38133

Re: __getattr__ Confusion

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <saul.spatz@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.05; 'class,': 0.07; 'differently': 0.07; 'python': 0.09; 'behave': 0.09; 'name):': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr :python-list': 0.10; 'looked': 0.10; 'def': 0.10; '(like': 0.15; 'lambda': 0.16; 'nonzero': 0.16; 'object):': 0.16; 'other:': 0.16; 'traceback.': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'instance,': 0.17; "shouldn't": 0.17; 'thanks,': 0.18; '>>>': 0.18; 'define': 0.20; 'question.': 0.20; 'skip:" 40': 0.20; 'sort': 0.21; 'bit': 0.21; 'cc:2**0': 0.23; 'academic': 0.23; 'monday,': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; '(most': 0.27; 'realize': 0.27; 'opposed': 0.27; 'sensible': 0.29; 'steven': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'file': 0.32; 'print': 0.32; 'getting': 0.33; 'defining': 0.33; 'traceback': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'wrong': 0.34; 'false': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'does': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'gives': 0.39; 'instead': 0.39; 'received:209.85.214': 0.39; 'skip:u 10': 0.60; 'situation': 0.62; 'between': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'want,': 0.65; 'person.': 0.69; 'special': 0.73; '2013': 0.84; 'case?': 0.84; 'classic:': 0.84; 'otten': 0.84; 'received:209.85.214.184': 0.84; 'received:mail-ob0-f184.google.com': 0.84; 'x):': 0.84
X-Received by 10.49.24.13 with SMTP id q13mr1865770qef.33.1359998947208; Mon, 04 Feb 2013 09:29:07 -0800 (PST)
Newsgroups comp.lang.python
Date Mon, 4 Feb 2013 09:29:07 -0800 (PST)
In-Reply-To <mailman.1322.1359987291.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=75.81.116.125; posting-account=En0C2QoAAABaZIRqpPv0m3w3Duh5br5m
References <095a0432-f8a4-40b4-96ed-1588896fba66@googlegroups.com> <510f3a91$0$29866$c3e8da3$5496439d@news.astraweb.com> <863c89b0-2125-4525-8ad8-1111b0a6beae@googlegroups.com> <mailman.1322.1359987291.2939.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 75.81.116.125
MIME-Version 1.0
Subject Re: __getattr__ Confusion
From Saul Spatz <saul.spatz@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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>
Message-ID <mailman.1328.1359998954.2939.python-list@python.org> (permalink)
Lines 125
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359998954 news.xs4all.nl 6933 [2001:888:2000:d::a6]:45975
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38133

Show key headers only | View raw


Thanks, Peter.  I realize this is getting sort of academic now, as I know how to do exactly what I want, but I'm still confused.  Is __getattr__ a special case then, even for classic classes?

class Adder():             # python 2.7, classic class
  def __init__(self, x):
    self.x = x
    self.__add__= lambda other: Adder(self.x+other.x)
    self.__getattr__ = lambda name: self.test(name)

  def __str__(self):
    return str(self.x)

  def test(self, name):
    print("Hello from test")
    raise AttributeError

x = Adder(3)
y = Adder(4)
print(x+y)
x.junk()

7
Traceback (most recent call last):
  File "C:\Users\Saul\Documents\PythonProjects\test.py", line 18
AttributeError: Adder instance has no attribute 'junk'

Why does this work for __add__ and not for __getattr__?

Of course, they both work if I write instead

def __add__self, other):
   return Adder(self.x+other.x)

def __getattr__(self, name):
   print(name)
   raise AttributeError

like a sensible person.

Saul  

On Monday, February 4, 2013 8:15:47 AM UTC-6, Peter Otten wrote:
> 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 | 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