executing a function/method from a variable

Y

Yves

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.
 
C

Carsten Haese

Yves said:
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,
 
J

Jon Clements

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.

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.
 
Y

Yves

Carsten said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,472
Messages
2,571,834
Members
48,802
Latest member
shadowoftheunknown
Top