Re: __getattr__ Confusion

2013-02-04 Thread Chris Angelico
On Tue, Feb 5, 2013 at 4:29 AM, Saul Spatz wrote: > class Adder(): # python 2.7, classic class > > Why does this work for __add__ and not for __getattr__? Is this a case of "why bother trying to understand it, just use new-style classes"? They do make more sense in many ways. ChrisA

Re: __getattr__ Confusion

2013-02-04 Thread Peter Otten
Saul Spatz wrote: > 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? Well, it never occured to me to try a per-instance __getattr__(), but you are ab

Re: __getattr__ Confusion

2013-02-04 Thread Saul Spatz
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.

Re: __getattr__ Confusion

2013-02-04 Thread Steven D'Aprano
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

Re: __getattr__ Confusion

2013-02-04 Thread Peter Otten
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 co

Re: __getattr__ Confusion

2013-02-04 Thread Saul Spatz
On Sunday, February 3, 2013 10:35:30 PM UTC-6, Steven D'Aprano wrote: > On Sun, 03 Feb 2013 17:08:47 -0800, Saul Spatz wrote: > > > > > I don't understand what's going on at all. Can't I dynamically define > > > __getattr__? How should I go about it? > > > > Special "dunder" methods (Dou

Re: __getattr__ Confusion

2013-02-03 Thread Steven D'Aprano
On Sun, 03 Feb 2013 17:08:47 -0800, Saul Spatz wrote: > I don't understand what's going on at all. Can't I dynamically define > __getattr__? How should I go about it? Special "dunder" methods (DoubleUNDERscore) are looked up only on the class, not on instances. That means that if you try to

Re: __getattr__ Confusion

2013-02-03 Thread Terry Reedy
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: Tr

Re: __getattr__ Confusion

2013-02-03 Thread Chris Angelico
On Mon, Feb 4, 2013 at 12:08 PM, Saul Spatz wrote: > class ScrolledCanvas(Frame): > def __init__(self, master, width, height, bg, cursor): > canv = self.canvas = Canvas(self, bg=bg, relief=SUNKEN) > > def __getattr__(self, name): > return getattr(self.canvas, name) Trying to get my he

__getattr__ Confusion

2013-02-03 Thread Saul Spatz
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 = Can