Learning to use decorators with classes

sk8in_zombi at yahoo.com.au sk8in_zombi at yahoo.com.au
Tue Jun 30 11:02:30 EDT 2009


--- On Tue, 30/6/09, Bruno Desthuilliers <bruno.42.desthuilliers at websiteburo.invalid> wrote:

> <ot>
> Don't use '__name__', they are reserved for the
> implementation. And FWIW, don't use '__name' unless you have
> a really compelling reason to do so.
> </ot>

That was an honest mistake!. Noted :)
> 
> >     def search(self, **kw):
> >         print
> 'Searching'
> >         ...
> 
> 
> This can't work, and it's a FAQ FWIW - but since there's no
> official c.l.py FAQ, we won't hold it against you !-)
> 

Can you please point me to the FAQ related to this snippet. I would be grateful.

> def and class are both *executable* statements (yes,
> classes and functions creation - like almost anything in
> Python - are run-time operations).
> 
> The first one creates a function object - *wherever* it
> happens - and bind the function object to the function's
> name in the current namespace. Think of it as an equivalent
> of the following javascript snippet:
> 
>    var func_name = function(arg) { /*
> function's body */ };
> 
> The second statement - class - builds a class object from
> the names defined in the class statement's body (that is,
> names defined in the class statement's body will become
> attributes of the class object).
> 
> Now about the 'methods' and 'self' stuff...
> 
> First understand that there's *nothing* magical with
> 'self'. It's *not* a keyword. It's only a naming convention,
> and you could use any legal Python identified instead.
> 
> The reason we have to explicitly mention it as first
> argument of the function is that it's the only way the
> function's body can get access to the current instance. What
> happens is (overly simplified) that during attribute
> resolution, when the found attribute happens to be a
> function,  this function is wrapped - together with the
> instance on which the attribute was looked up and it's class
> -  into a callable method object. Then when you call
> this method object, it inserts the instance as first
> argument to the function call, and returns the result of the
> function call (if you want to read more about this and how
> computed attributes are implemented in Python, google for
> 'descriptor protocol').
> 
> IOW, and to make a long story short, calling
> instance.method is the same as calling
> Class.method(instance).
> 
>

Thank you very much for your explanation. I will remember these points by heart.
 
> Ok, now to the point: when you call getConnection within
> the class statement's body, there's no magical "self"
> keyword poiting to an instance, and since the class itself
> doesn't yet exists, so there's *no* way you could get at an
> instance of it anyway !-)
> 
> There are many ways to solve your problem, the simplest
> bing probably to write another decorator calling on the
> first one, ie:
> 
> 
> def connected_method(func):
>     def connected(self, *args, **kw):
>         wrapped =
> getConnection(self.__this, self.__that)(func)
>         return wrapped(*args, **kw)
> 
>     return connected
> 
> 
> Note that this may not be that efficient - you'll have
> quite a few function calls involved here.
>

I was trying follow the concept of decorators with arguments and I can now understand why it failed, thanks. 

Thanks and regards,
SZ



      Access Yahoo!7 Mail on your mobile. Anytime. Anywhere.
Show me how: http://au.mobile.yahoo.com/mail



More information about the Python-list mailing list