Decorators for multimethods

Discussion in 'Python' started by Roman Suzi, Dec 10, 2004.

  1. Roman Suzi

    Roman Suzi Guest

    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.

    #!/bin/env python2.4
    if "We have Neel Krishnaswami module Multimethod.py":

    import Multimethod

    class Foo: pass

    class Bar(Foo): pass

    def multimethod(g, *args):
    def make_multimethod(func):
    mm = Multimethod.Method(tuple(args), func)
    g.add_method(mm)
    return mm
    return make_multimethod

    g = Multimethod.Generic()

    @multimethod(g, Foo, Foo)
    def m1(a, b): return 'foofoo'

    @multimethod(g, Foo, Bar)
    def m2(a, b): return 'foobar'

    @multimethod(g, Bar, Foo)
    def m3(a, b): return 'barfoo'

    try:
    print 'Argtypes ', 'Result'
    print 'Foo, Foo:', g(Foo(), Foo())
    print 'Foo, Bar:', g(Foo(), Bar())
    print 'Bar, Foo:', g(Bar(), Foo())
    print 'Bar, Bar:', g(Bar(), Bar())
    except Multimethod.AmbiguousMethodError:
    print 'Failed due to AmbiguousMethodError'


    Sincerely yours, Roman Suzi
    --
    =\= My AI powered by GNU/Linux RedHat 7.3
    Roman Suzi, Dec 10, 2004
    #1
    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. Michael Strorm
    Replies:
    12
    Views:
    1,769
    Michael Strorm
    Apr 20, 2005
  2. Michele Simionato

    decorators and multimethods

    Michele Simionato, Aug 7, 2004, in forum: Python
    Replies:
    8
    Views:
    391
    David Fraser
    Aug 19, 2004
  3. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Replies:
    11
    Views:
    567
    Arien Malec
    Aug 16, 2004
  4. Gerardo Herzig

    multimethods decorator

    Gerardo Herzig, Oct 10, 2007, in forum: Python
    Replies:
    4
    Views:
    677
    Gerardo Herzig
    Oct 11, 2007
  5. itaj sherman
    Replies:
    39
    Views:
    1,107
    Larry Evans
    Feb 25, 2011
Loading...

Share This Page