473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1328
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Michele Simionato | last post by:
Decorators can generate endless debate about syntax, but can also be put to better use ;) Actually I was waiting for decorators to play a few tricks that were syntactically too ugly to be even...
41
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
14
by: Gabriel Zachmann | last post by:
This post is not strictly Python-specific, still I would like to learn other university teachers' opinion. Currently, I'm teaching "introduction to OO programming" at the undergrad level. My...
0
by: Roman Suzi | last post by:
hi! I've found one more nice use case for decorators. I feel multimethods could be made even nicier by defining multimethods inside special class. But I have not figured out how to do it yet. ...
30
by: Ron_Adam | last post by:
I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a...
22
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
3
by: Ron_Adam | last post by:
Ok... it's works! :) So what do you think? Look at the last stacked example, it process the preprocess's first in forward order, then does the postprocess's in reverse order. Which might be...
5
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
4
by: thomas.karolski | last post by:
Hi, I would like to create a Decorator metaclass, which automatically turns a class which inherits from the "Decorator" type into a decorator. A decorator in this case, is simply a class which...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.