471,342 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

multimethods decorator

Hi all. Im reading the Gido's aproach using decorators at
http://www.artima.com/weblogs/viewpo...?thread=101605

It looks good to me, but the examples shows the functionality using
functions.
Now, when i try to give this decorator into a method, if i try the

class test(object):
@multimethod(...)
def met(self, ...):

The multimethod decorator needs the types of the arguments, and, if the
met method requires self as the first argument, the multimethod should
look like
@multimethod(self.__class__, bla, ble) or some like that...

Now i know that im wrong, because i have this error
>@multimethod(self.__class__)
NameError: name 'self' is not defined
So what would be the first argument to @multimethod??

Thanks!!
Gerardo
Oct 10 '07 #1
4 1268
Gerardo Herzig a écrit :
Hi all. Im reading the Gido's aproach using decorators at
http://www.artima.com/weblogs/viewpo...?thread=101605

It looks good to me, but the examples shows the functionality using
functions.
Now, when i try to give this decorator into a method, if i try the

class test(object):
@multimethod(...)
def met(self, ...):

The multimethod decorator needs the types of the arguments, and, if the
met method requires self as the first argument, the multimethod should
look like
@multimethod(self.__class__, bla, ble) or some like that...

Now i know that im wrong, because i have this error
>@multimethod(self.__class__)
>NameError: name 'self' is not defined
Indeed. Neither self (which will only be known at method call time) nor
even the 'test' class (which is not yet defined when the decorator is
executed) are availables.
So what would be the first argument to @multimethod??
A string ?-)

FWIW, there's already an implementation of multiple dispacth by Mr. Eby...
Oct 10 '07 #2
Gerardo Herzig a écrit :
>Hi all. Im reading the Gido's aproach using decorators at
http://www.artima.com/weblogs/viewpo...?thread=101605

It looks good to me, but the examples shows the functionality using
functions.
Now, when i try to give this decorator into a method, if i try the

class test(object):
@multimethod(...)
def met(self, ...):

The multimethod decorator needs the types of the arguments, and, if the
met method requires self as the first argument, the multimethod should
look like
@multimethod(self.__class__, bla, ble) or some like that...

Now i know that im wrong, because i have this error
> >@multimethod(self.__class__)
NameError: name 'self' is not defined

Indeed. Neither self (which will only be known at method call time) nor
even the 'test' class (which is not yet defined when the decorator is
executed) are availables.
Doh!
>
>So what would be the first argument to @multimethod??

A string ?-)
Ah? And what will that string contains?
>
FWIW, there's already an implementation of multiple dispacth by Mr. Eby...
Oh yes, i found the dispatch version of multimethods, but i have not tried
it yet. Do you think is better this version than Guido's?

Thanks!
Gerardo

Oct 11 '07 #3
gh*****@fmed.uba.ar a écrit :
>Gerardo Herzig a écrit :
>>Hi all. Im reading the Gido's aproach using decorators at
http://www.artima.com/weblogs/viewpo...?thread=101605

It looks good to me, but the examples shows the functionality using
functions.
Now, when i try to give this decorator into a method, if i try the

class test(object):
@multimethod(...)
def met(self, ...):

The multimethod decorator needs the types of the arguments, and, if the
met method requires self as the first argument, the multimethod should
look like
@multimethod(self.__class__, bla, ble) or some like that...

Now i know that im wrong, because i have this error
>@multimethod(self.__class__)
>NameError: name 'self' is not defined
Indeed. Neither self (which will only be known at method call time) nor
even the 'test' class (which is not yet defined when the decorator is
executed) are availables.
Doh!
If you're surprised, then you'd better learn way more about Python's
internal (execution model && object model mostly) before continuing with
multimethods.
>>So what would be the first argument to @multimethod??
A string ?-)
Ah? And what will that string contains?
What makes sens for you. Don't forget that Python has very strong
introspection features. Also note that it's not uncommon to use a 2-pass
approach : marking some methods with the decorator, then doing the real
processing in the metaclass (which of course implies a custom metaclass)
or in the __new__ method (in which case this processing will happen on
*each* instanciation).
>FWIW, there's already an implementation of multiple dispacth by Mr. Eby...
Oh yes, i found the dispatch version of multimethods, but i have not tried
it yet. Do you think is better this version than Guido's?
I think that dispatch is actually used in a few packages, frameworks or
applications. Is it the case of Guido's stuff ?

Also, IIRC, Guido's snippet is quite less generic than dispatch (which
is based on expression rules, not only on types).

My 2 cents...
Oct 11 '07 #4
Bruno Desthuilliers wrote:
>gh*****@fmed.uba.ar a écrit :

>>>Gerardo Herzig a écrit :
Hi all. Im reading the Gido's aproach using decorators at
http://www.artima.com/weblogs/viewpo...?thread=101605

It looks good to me, but the examples shows the functionality using
functions.
Now, when i try to give this decorator into a method, if i try the

class test(object):
@multimethod(...)
def met(self, ...):

The multimethod decorator needs the types of the arguments, and, if the
met method requires self as the first argument, the multimethod should
look like
@multimethod(self.__class__, bla, ble) or some like that...

Now i know that im wrong, because i have this error
@multimethod(self.__class__)
NameError: name 'self' is not defined
Indeed. Neither self (which will only be known at method call time) nor
even the 'test' class (which is not yet defined when the decorator is
executed) are availables.



>>Doh!


If you're surprised, then you'd better learn way more about Python's
internal (execution model && object model mostly) before continuing with
multimethods.

Is not that, is just...it allways make sense to me AFTER someone tells
me!! :)
>
>>>>So what would be the first argument to @multimethod??
A string ?-)



>>Ah? And what will that string contains?


What makes sens for you. Don't forget that Python has very strong
introspection features. Also note that it's not uncommon to use a 2-pass
approach : marking some methods with the decorator, then doing the real
processing in the metaclass (which of course implies a custom metaclass)
or in the __new__ method (in which case this processing will happen on
*each* instanciation).

Oh well, now you kill me with that one.
>
>>>FWIW, there's already an implementation of multiple dispacth by Mr. Eby...

Oh yes, i found the dispatch version of multimethods, but i have not tried
it yet. Do you think is better this version than Guido's?


I think that dispatch is actually used in a few packages, frameworks or
applications. Is it the case of Guido's stuff ?

Also, IIRC, Guido's snippet is quite less generic than dispatch (which
is based on expression rules, not only on types).

Ok. So im giving dispatch version a chance. Its working nice so far.
Thanks Man!
Gerardo
Oct 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Michele Simionato | last post: by
41 posts views Thread by John Marshall | last post: by
14 posts views Thread by Gabriel Zachmann | last post: by
reply views Thread by Roman Suzi | last post: by
30 posts views Thread by Ron_Adam | last post: by
22 posts views Thread by Ron_Adam | last post: by
3 posts views Thread by Ron_Adam | last post: by
4 posts views Thread by thomas.karolski | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.