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_multimetho d(func):
mm = Multimethod.Met hod(tuple(args) , func)
g.add_method(mm )
return mm
return make_multimetho d
g = Multimethod.Gen eric()
@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.Amb iguousMethodErr or:
print 'Failed due to AmbiguousMethod Error'
Sincerely yours, Roman Suzi
--
rn*@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3