Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > dispatch tables in Python 2.4

Reply
Thread Tools

dispatch tables in Python 2.4

 
 
Michele Simionato
Guest
Posts: n/a
 
      08-07-2004
<Still experimenting with the new features of Python 2.4 ...>

A thing I always had in my wishlist was the ability to define dispatch
tables in an elegant way. Currently, Python is butt ugly:

def function_with_a_long_name_1():
"do this"

def function_with_a_long_name_2():
"do that"

dispatch_table = dict(
function_with_a_long_name_1 = function_with_a_long_name_1,
function_with_a_long_name_2 = function_with_a_long_name_2,
)

Really horrible and error prone (especially for a poor typist like
myself). With decorators I can do

def add_to(lst):
def closure(func):
lst.append(func)
return func
return closure

lst = []

@add_to(lst)
def function_with_a_long_name_1():
"do this"

@add_to(lst)
def function_with_a_long_name_2():
"do that"

dispatch_table = dict((f.func_name,f) for f in lst)
#!generator-comprehension!

With the advent of decorators I see myself using more and more
closures.

BTW, what's the status of the so called "functional" module? I think
there
was a rumor of such a module scheduled for Python 2.4 or 2.5. To play
with
decorators we really need some standard way to compose functions and
to do
partial application of arguments.

Just my 2c,


Michele Simionato

P.S. decorators make also easy to abuse classes for making dispatch
tables:

class DispatchTable(object):

@staticmethod
def function_with_a_long_name_1():
"do this"

@staticmethod
def function_with_a_long_name_2():
"do that"


But I somewhat prefer dispatch_table[function_name] over
getattr(DispatchTable, function_name).
 
Reply With Quote
 
 
 
 
Paul Moore
Guest
Posts: n/a
 
      08-07-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Michele Simionato) writes:

> BTW, what's the status of the so called "functional" module? I think
> there was a rumor of such a module scheduled for Python 2.4 or 2.5.
> To play with decorators we really need some standard way to compose
> functions and to do partial application of arguments.


PEP 309 is marked as "Accepted", and an implementation patch exists.
The PEP needs updating, and there are possibly a few more procedural
things to do before the code is checked in. I emailed the author of
the PEP on Friday, asking about this. I think it's mainly lack of
tuits.

The PEP only covers partial application, BTW.

Paul.
--
Any sufficiently advanced technology is indistinguishable from magic.
-- Arthur C. Clarke
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with understanding/using dispatch tables Mothra Perl Misc 2 04-12-2007 08:00 PM
php to python dynamic dispatch example todddeluca@gmail.com Python 0 12-18-2005 07:45 PM
How many virtual functions before needing dispatch tables? The Stevemeister C++ 4 11-15-2004 07:21 PM
Tables within tables Otuatail HTML 7 08-02-2004 04:42 AM
Text after 2 tables actually appears between 2 tables ! Peter Bassett HTML 3 08-15-2003 06:46 PM



Advertisments