dispatch tables in Python 2.4

Discussion in 'Python' started by Michele Simionato, Aug 7, 2004.

  1. <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).
    Michele Simionato, Aug 7, 2004
    #1
    1. Advertising

  2. Michele Simionato

    Paul Moore Guest

    (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
    Paul Moore, Aug 7, 2004
    #2
    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. Peter Bassett
    Replies:
    3
    Views:
    924
    Augustus
    Aug 15, 2003
  2. Otuatail

    Tables within tables

    Otuatail, Jul 31, 2004, in forum: HTML
    Replies:
    7
    Views:
    494
  3. The Stevemeister
    Replies:
    4
    Views:
    348
    Mike Smith
    Nov 15, 2004
  4. Replies:
    0
    Views:
    594
  5. Mothra
    Replies:
    2
    Views:
    101
    Mothra
    Apr 12, 2007
Loading...

Share This Page