__getattr__ Confusion

Chris Angelico rosuav at gmail.com
Mon Feb 4 02:28:43 CET 2013

On Mon, Feb 4, 2013 at 12:08 PM, Saul Spatz <saul.spatz at gmail.com> 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 head around what you're doing here. Why are you
inheriting Frame, but passing all attribute queries through to the
Canvas? Why not inherit Canvas?

It looks to me like you're going to have some kind of bootstrap
problem no matter how you do it. You're creating a cyclic reference
(you pass self to Canvas), so one way or another, you need to start
the loop.

Dunder methods (like __getattr__) are looked up in the class, not the
instance, so you can't simply set it in the way you describe. I think
your best bet is going to be the "set up a stub, then fill in the
details" method, which is more or less what you're doing (a stubby


More information about the Python-list mailing list