Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python
decorators and multimethods
 

Index | Next | Previous | View Flat


michele.simionato at gmail

Aug 7, 2004, 12:40 AM


Views: 521
Permalink
decorators and multimethods

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 imaginable for Python 2.3.

One trick is to use decorators to implement multimethods. A while ago
Howard Stearns posted here a recipe to implement generic functions
a.k.a multimethods.

I have not studied his recipe, so don't ask me how it works. All I
did was to add an "addmethod" function and save his code in a module
called genericfunctions.

Decorators allowed me to use the following syntax:

# BEGIN generic functions in Python, example
# use code and examples from Howard Stearns

from genericfunctions import Generic_Function, addmethod

foo = Generic_Function()

@addmethod(object, object, object)
def foo(_, x, y, z):
return 'default'

@addmethod(int, int, int)
def foo(call_next, x, y, z):
return 'all ints , ' + call_next(x, y, z)

@addmethod(object, object)
def foo( _, x, y):
return 'just two'

print foo # the multimethod table as a dictionary
print foo(1, 2, 'three') # => default
print foo(1, 2, 3) # => all ints, default
print foo(1, 'two') #> just two
print foo('oops') #=> genericfunctions.NoNextMethod error

# END generic functions in Python, example

Howard Stearns' code is posted here
http://groups.google.it/groups?hl=it&lr=&ie=UTF-8&threadm=40C3BC71.5050206%40charter.net&rnum=1&prev=/groups%3Fq%3Dhoward%2Bstearns%2Bgroup:comp.lang.python.*%26hl%3Dit%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.python.*%26selm%3D40C3BC71.5050206%2540charter.net%26rnum%3D1

I just added the following function:

import sys

def addmethod(*types):
"""sys._getframe hack; works when the generic function is defined
in the globals namespace."""
caller_globs = sys._getframe(1).f_globals
def function2generic(f):
generic = caller_globs[f.func_name]
generic[types] = f
return generic
return function2generic

Decorators did all the rest ;) Just to add an use case I haven't
seen before.

Michele Simionato
--
http://mail.python.org/mailman/listinfo/python-list

Subject User Time
decorators and multimethods michele.simionato at gmail Aug 7, 2004, 12:40 AM
    Re: decorators and multimethods howard.stearns at charter Aug 7, 2004, 6:12 AM
    Re: decorators and multimethods martin at v Aug 7, 2004, 6:51 AM
    Re: decorators and multimethods martin at v Aug 7, 2004, 7:18 AM
    Re: decorators and multimethods michele.simionato at gmail Aug 7, 2004, 8:42 AM
        Re: decorators and multimethods ronaldoussoren at mac Aug 7, 2004, 9:29 AM
        Re: decorators and multimethods davidf at sjsoft Aug 19, 2004, 2:13 AM
    Re: decorators and multimethods michele.simionato at gmail Aug 8, 2004, 6:31 AM
    Re: decorators and multimethods pje at telecommunity Aug 14, 2004, 12:02 PM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.