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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • python@bdurham.com

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

    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
  • Arnaud Delobelle

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

    On 29 Apr, 13:10, pyt...@bdurham. com 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

    Comment

    • python@bdurham.com

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

      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>

      Comment

      • python@bdurham.com

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

        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

        Comment

        Working...