getattr on a function

Peter Otten __peter__ at web.de
Mon Apr 27 13:15:42 EDT 2009


Mr SZ wrote:

> Is it possible to call functions using getattr. I have written a simple
> script with functions that call either SSL, TLS or plain functionality.
> 
> something like:
> def func():
>   ...
> 
> def funcSSL():
>   ...
> 
> def funcTLS():
>   ...
> 
> Now, based on my args I would like to call either one of them. In my case,
> I can't seem to figure out what my object would be when I call
> getattr(object, 'func'+<encryption>) !

>From within the module:

encryption = ...
f = globals()["func" + encryption]
f(...)


In other modules, assuming the module containing the function is
called 'module':

import module

encryption = ...
f = getattr(module, "func" + encryption)
f(...)

Peter



More information about the Python-list mailing list