multimethods decorator

Discussion in 'Python' started by Gerardo Herzig, Oct 10, 2007.

  1. Hi all. Im reading the Gido's aproach using decorators at
    http://www.artima.com/weblogs/viewpost.jsp?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
    Gerardo Herzig, Oct 10, 2007
    #1
    1. Advertising

  2. Gerardo Herzig a écrit :
    > Hi all. Im reading the Gido's aproach using decorators at
    > http://www.artima.com/weblogs/viewpost.jsp?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...
    Bruno Desthuilliers, Oct 10, 2007
    #2
    1. Advertising

  3. Gerardo Herzig

    Guest

    > Gerardo Herzig a écrit :
    >> Hi all. Im reading the Gido's aproach using decorators at
    >> http://www.artima.com/weblogs/viewpost.jsp?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, 2007
    #3
  4. a écrit :
    >> Gerardo Herzig a écrit :
    >>> Hi all. Im reading the Gido's aproach using decorators at
    >>> http://www.artima.com/weblogs/viewpost.jsp?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...
    Bruno Desthuilliers, Oct 11, 2007
    #4
  5. Bruno Desthuilliers wrote:

    > a écrit :
    >
    >
    >>>Gerardo Herzig a écrit :
    >>>
    >>>
    >>>>Hi all. Im reading the Gido's aproach using decorators at
    >>>>http://www.artima.com/weblogs/viewpost.jsp?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
    Gerardo Herzig, Oct 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean Lutrin
    Replies:
    8
    Views:
    623
    Chris Uppal
    Nov 18, 2004
  2. Michele Simionato

    decorators and multimethods

    Michele Simionato, Aug 7, 2004, in forum: Python
    Replies:
    8
    Views:
    384
    David Fraser
    Aug 19, 2004
  3. Roman Suzi

    Decorators for multimethods

    Roman Suzi, Dec 10, 2004, in forum: Python
    Replies:
    0
    Views:
    269
    Roman Suzi
    Dec 10, 2004
  4. glomde
    Replies:
    5
    Views:
    511
    glomde
    Mar 29, 2007
  5. itaj sherman
    Replies:
    39
    Views:
    1,083
    Larry Evans
    Feb 25, 2011
Loading...

Share This Page