Does python have a function like call_user_func()
in PHP?
PHP Version:
call_user_func(array($object,$methodName),$parameters)
How do I achieve the above in Python?
Does python have a function like call_user_func()
in PHP?
PHP Version:
call_user_func(array($object,$methodName),$parameters)
How do I achieve the above in Python?
I don't see the problem, unless methodName
is a string. In that case getattr does the job:
>>> class A:
... def func(self, a, b):
... return a + b
...
>>> a = A()
>>> getattr(a, 'func')(2, 3)
5
If object
is also a string, then this would work, using globals or locals (but then you may have other, bigger, problems):
>>> getattr(locals()['a'], 'func')(2, 3)
5
>>> getattr(globals()['a'], 'func')(2, 3)
5
Edit: re your clarification. To initialise an object based on a string:
>>> class A:
... def __init__(self): print('a')
...
>>> class B:
... def __init__(self): print('b')
...
>>> clsStr = 'A'
>>> myObj = locals()[clsStr]()
a
I am not sure if this is really what you want though... unless you have many different classes, why not just perform string matching?
Another edit: Though the above works, you should seriously consider going with a solution such as provided by Ignacio Vazquez-Abrams. For one thing, by storing all possible classes in a dict
, you avoid strange behaviour that may result from passing an incorrect string argument which just happens to match the name of a non-related class in the current scope.
For a given object 'obj', a given method name 'meth', and a given set of arguments 'args':
obj.__getattribute__(meth)(*args)
Should get you there.
Of course, it goes without saying - What the heck is it you want to do?
You can call use getattr with locals() or globals() to do something similar
# foo.method() is called if it's available in global scope
getattr(globals()['foo'], 'method')(*args)
# Same thing, but for 'foo' in local scope
getattr(locals()['foo'], 'method')(*args)
See also: Dive Into Python on getattr(), on locals() and globals()
Picking which object to instantiate isn't that hard. A class is a first-class object and can be assigned to a variable or passed as an argument to a function.
class A(object):
def __init__( self, arg1, arg2 ):
etc.
class B(object):
def __init__( self, arg1, arg2 ):
etc.
thing_to_make = A
argList= ( some, pair )
thing_to_make( *argList )
thing_to_make = B
argList- ( another, pair )
thing_to_make( *argList )
def doSomething( class_, arg1, arg2 ):
thing= class_( arg1, arg2 )
thing.method()
print thing
All works nicely without much pain. You don't need a "call_user_function" sort of thing in Python
If you need to use classes from far-off places (and in fact, if you need any classes at all) then you're best off creating and using a dictionary for them:
funcs = {'Eggs': foo.Eggs, 'Spam': bar.Spam}
def call_func(func_name, *args, **kwargs):
if not func_name in funcs:
raise ValueError('Function %r not available' % (func_name,))
return funcs[func_name](*args, **kwargs)