Calling a method with a variable name

Carsten Haese carsten.haese at gmail.com
Wed Nov 4 10:04:55 EST 2009


Simon Mullis wrote:
> def main():
>     stats_obj = Statistic()
>     name = re.sub("[^A-Za-z]", "", sys.argv[0])
>     method = getattr(stats_obj, name, None)
>     if callable(method):
>         stats_obj.name()              #  <------------HERE
>     else:
>         print "nope, not sure what you're after...."
> -----------
> 
> However, as I'm sure you've all noticed already, there is no method
> called "name". I would really prefer to get a nudge in the right
> direction before I start evaling variables and so on.

At the point you marked "HERE", you've already found the method, and you
have determined that it is callable. You just need to call it. Like
this: method().

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net




More information about the Python-list mailing list