Calling a method with a variable name

Simon Mullis simon at mullis.co.uk
Wed Nov 4 16:20:50 CET 2009


May I be the first to say "Doh!"

Problem solved, many thanks to both Carsten and Diez!

SM

2009/11/4 Carsten Haese <carsten.haese at gmail.com>:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Simon Mullis
_________________
simon at mullis.co.uk



More information about the Python-list mailing list