calling a method using a variable name?
Martin v. Löwis
martin at v.loewis.de
Fri Mar 14 18:21:52 EST 2003
Patrick Price <no-email at nowhere.com> writes:
> class X:
> def method():
> pass
>
>
> z=X."method"()
>
> How would I do this?
Notice that writing
z = X.method()
wouldn't work, either, since you cannot call an unbound method
without an instance. So you would have to write
x = X()
z = x.method()
If you want to use a string, write
z = getattr(x, "method") ()
HTH,
Martin
More information about the Python-list
mailing list