Re: Given a string - execute a function by the same name

Discussion in 'Python' started by python@bdurham.com, Apr 29, 2008.

  1. Guest

    Bruno,

    Thank you for your detailed analysis. I learned a lot about Python
    reading everyone's responses.

    For development I'm using #5: "globals().get("func")" because its
    seamless to add additional functionality.

    But when I release into production I'm going to shift to #3: "Place all
    my functions in dictionary and lookup the function to be called". This
    technique will allow me to precisely control the dynamic nature of my
    application.

    Thanks again to everyone who contributed on this thread.

    Regards,
    Malcolm
    , Apr 29, 2008
    #1
    1. Advertising

  2. On 29 Apr, 13:10, wrote:
    > Bruno,
    >
    > Thank you for your detailed analysis. I learned a lot about Python
    > reading everyone's responses.
    >
    > For development I'm using #5: "globals().get("func")" because its
    > seamless to add additional functionality.
    >
    > But when I release into production I'm going to shift to #3: "Place all
    > my functions in dictionary and lookup the function to be called". This
    > technique will allow me to precisely control the dynamic nature of my
    > application.
    >
    > Thanks again to everyone who contributed on this thread.
    >
    > Regards,
    > Malcolm


    You could avoid #5 from the start using a decorator:

    functions = {}

    def register(func):
    functions[func.__name__] = func
    return func

    @register
    def foo(): print "Foo!"

    @register
    def bar(): print "Bar!"


    >>> functions

    {'foo': <function foo at 0x6f2f0>, 'bar': <function bar at 0x6f330>}
    >>> functions['bar']()

    Bar!

    --
    Arnaud
    Arnaud Delobelle, Apr 29, 2008
    #2
    1. Advertising

  3. Guest

    Arnaud,

    Just when I thought my solution couldn't get any better :)

    Thanks for that great tip and for an excellent demonstration of using a
    decorator.

    Regards,
    Malcolm

    <snip>
    You could avoid #5 from the start using a decorator:

    functions = {}

    def register(func):
    functions[func.__name__] = func
    return func

    @register
    def foo(): print "Foo!"

    @register
    def bar(): print "Bar!"


    >>> functions

    {'foo': <function foo at 0x6f2f0>, 'bar': <function bar at 0x6f330>}
    >>> functions['bar']()

    Bar!
    </snip>
    , Apr 29, 2008
    #3
  4. Guest

    Erik,

    > Perhaps I missed something earlier in the thread, but I really don't see the need for that registry dict or the register decorator. Python already

    maintains a dictionary for each scope:

    The advantage of the decorator technique is that you explicitly declare
    which functions are eligible for execution.

    Using locals() is too broad because it might allow a user to execute an
    unintended function.

    Malcolm
    , Apr 29, 2008
    #4
    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. Lord0
    Replies:
    1
    Views:
    562
    Thomas Weidenfeller
    Apr 19, 2006
  2. Andrzej
    Replies:
    7
    Views:
    419
    Andrzej
    May 23, 2005
  3. chiara
    Replies:
    6
    Views:
    465
    Barry Schwarz
    Oct 6, 2005
  4. 2Barter.net
    Replies:
    0
    Views:
    363
    2Barter.net
    Dec 13, 2006
  5. Replies:
    6
    Views:
    273
Loading...

Share This Page