Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > executing a function/method from a variable

Reply
Thread Tools

executing a function/method from a variable

 
 
Yves
Guest
Posts: n/a
 
      10-17-2009
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/
 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      10-17-2009
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

 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      10-17-2009
On Oct 17, 3:02*am, Yves <(E-Mail Removed)> 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.
 
Reply With Quote
 
Yves
Guest
Posts: n/a
 
      10-17-2009
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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-17-2009
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing code in a variable Zangief Ief Ruby 9 04-20-2008 05:13 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Variable 'variable lengths' Taras_96 VHDL 2 05-09-2005 12:58 AM
Convert Character Variable to Integer Variable Brad Smallridge VHDL 2 11-18-2004 01:56 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments