executing a function/method from a variable

Discussion in 'Python' started by Yves, Oct 17, 2009.

  1. Yves

    Yves Guest

    What is the best way to execute a function which name is stored in a variable ?

    Right now I use an eval, but I'm wondering if there isn't a better way:



    Here is a simplified example, but what I use this for is to parse a formated
    text file, and execute a different method depending on the pattern:

    import sys


    class dummy(object):
    def __init__(self, arg):
    self.todo = 'self.print' + arg;

    def printa(self):
    print 'a'

    def printb(self):
    print 'b'

    def doit(self):
    #k = eval(self.todo)
    #k()

    eval(self.todo)()



    o = dummy(sys.argv[1])
    o.doit()

    Thanks.

    --
    Yves.
    http://www.sollers.ca/
     
    Yves, Oct 17, 2009
    #1
    1. Advertising

  2. Yves wrote:
    > Right now I use an eval, but I'm wondering if there isn't a better way:


    There's (almost) always a better way than using eval. In this case, you
    should use getattr().

    Here's your simplified example modified to use getattr:

    import sys

    class dummy(object):
    def __init__(self, arg):
    self.todo = "print"+arg

    def printa(self):
    print 'a'

    def printb(self):
    print 'b'

    def doit(self):
    func = getattr(self, self.todo)
    func()

    o = dummy(sys.argv[1])
    o.doit()

    HTH,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, Oct 17, 2009
    #2
    1. Advertising

  3. Yves

    Jon Clements Guest

    On Oct 17, 3:02 am, Yves <> wrote:
    > What is the best way to execute a function which name is stored in a variable ?
    >
    > Right now I use an eval, but I'm wondering if there isn't a better way:
    >
    > Here is a simplified example, but what I use this for is to parse a formated
    > text file, and execute a different method depending on the pattern:
    >
    > import sys
    >
    > class dummy(object):
    >    def __init__(self, arg):
    >      self.todo = 'self.print' + arg;
    >
    >    def printa(self):
    >      print 'a'
    >
    >    def printb(self):
    >      print 'b'
    >
    >    def doit(self):
    >      #k = eval(self.todo)
    >      #k()
    >
    >      eval(self.todo)()
    >
    > o = dummy(sys.argv[1])
    > o.doit()
    >
    > Thanks.
    >
    > --
    > Yves.http://www.sollers.ca/


    Depending on your exact requirements, take a look at pyparsing.

    Has (from my experience) a small learning curve and is a
    useful library addition!

    It could well be overkill, but not knowing your exact requirements,
    it'd be worth looking at anyway.

    Jon.
     
    Jon Clements, Oct 17, 2009
    #3
  4. Yves

    Yves Guest

    Carsten Haese wrote:

    >> Right now I use an eval, but I'm wondering if there isn't a better way:

    >
    > There's (almost) always a better way than using eval.


    Yes I agree, every time I use eval, I feel like the language is missing
    something, fair enough to shell type languages, but usually not the case in
    python. That's why I asked,

    > In this case, you
    > should use getattr().


    Right ! I always forget about get/set attr. Thanks !

    >
    > Here's your simplified example modified to use getattr:

    Tried it, and it works if we remove the self from the name of course:

    import sys


    class dummy(object):
    def __init__(self, arg):
    self.todo = 'print' + arg;

    def printa(self):
    print 'a'

    def printb(self):
    print 'b'

    def doit(self):
    #func = getattr(self, self.todo)
    #func()
    getattr(self, self.todo)()


    o = dummy(sys.argv[1])
    o.doit()

    Thanks again,

    --
    Yves.
    http://www.SollerS.ca
     
    Yves, Oct 17, 2009
    #4
  5. Yves

    Terry Reedy Guest

    Yves wrote:
    > What is the best way to execute a function which name is stored in a
    > variable ?


    One standard way is a dict mapping names to function objects.

    >>> numfuncs = {'int':int, 'float':float}
    >>> f='int'
    >>> numfuncs[f]('33')

    33

    Since attributes gets mapped to a dict, this is similar to previous answer.

    tjr
     
    Terry Reedy, Oct 17, 2009
    #5
    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. mfglinux
    Replies:
    11
    Views:
    726
    Roberto Bonvallet
    Sep 12, 2007
  2. AikidoGuy
    Replies:
    11
    Views:
    575
    Seebs
    Nov 21, 2011
  3. Luca Scaljery
    Replies:
    6
    Views:
    86
    Luca Scaljery
    Feb 3, 2008
  4. Kyung won Cheon
    Replies:
    0
    Views:
    216
    Kyung won Cheon
    Nov 21, 2008
  5. David Filmer
    Replies:
    19
    Views:
    257
    Kevin Collins
    May 21, 2004
Loading...

Share This Page