Function creation (what happened?)

Viktor alefnula at gmail.com
Fri May 9 09:35:45 EDT 2008


I figured out the first two solutions, but the third looks like the
most cleaner, think I'll use that one...

Thank you everyone. :)

On May 9, 3:24 pm, George Sakkis <george.sak... at gmail.com> wrote:
> The decorator does receive the correct function. The problem is that
> at this point __init__ is still a plain function, not a method, i.e.
> the sequence is:
>    function -> decorated function -> method
>
> There are several workarounds if you really want to differentiate
> between functions and methods, none of them perfect:
>
> - Apply the decorator after the class has been built, i.e. after the
> functions have been wrapped in (unbound) methods:
>     A.__init__ = w(A.__init__)
>
> - (Risky Hack): Guess whether a function is intended to be wrapped in
> a method by checking whether its first argument is named "self".
> Obviously this is not foolproof and it doesn't work for static/class
> methods.
>
> - Have two different decorators, one intended for plain functions and
> one for functions-to-be-methods (e.g. @deco, @decomethod).
>
> George




More information about the Python-list mailing list