[Tutor] Calling a function by string name
Smith, Jeff
jsmith at medplus.com
Fri Jul 21 18:52:16 CEST 2006
I have an object and I want to call a method that I have constructed the
name for in a string.
For example:
str_method = 'myfun'
obj.str_method
Of course, this fails. I know I could probably do this with exec but is
there a better way?
For context, the specific application is a wrapper to calling p4python.
Note that the second return (under the else under the try) is where I
want to call 'method' which is the string name of a method valid for
self._client.
def _p4runner(self, method, *args):
'runs a Perforce command'
try:
if callable(method):
return method(*args)
else:
return self._client.method(*args)
except p4.P4Client.error:
raise
except:
if self._client.errors:
raise p4.P4Client.error('\n'.join(self._client.errors))
else:
raise
Clear as mud?
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060721/7181bc38/attachment-0001.html
More information about the Tutor
mailing list