Can Python do this?
Quinn Dunkan
quinn at barf.ugcs.caltech.edu
Mon Mar 4 16:08:05 EST 2002
On 04 Mar 2002 12:41:50 -0800, Paul Rubin <phr-n2002a at nightsong.com> wrote:
> x = my_class() # x is an instance of some class
> method_name = "FuncCall" + "1" # gives "FuncCall1"
> method = getattr(x, method_name) # find the method
> method (args) # call the method
>
>is preferable to using eval, if method_name is actually defined for
>my_class. However, if method_name is only defined for some class that
>my_class inherits from, and not from my_class itself, then this won't
>work.
Really?
Python 2.0 (#1, Oct 25 2000, 13:33:13)
[GCC 2.8.1] on irix6-n32
Type "copyright", "credits" or "license" for more information.
>>> class A:
... def m(self): pass
...
>>> class B(A):
... pass
...
>>> a = B()
>>> a
<__main__.B instance at 100e3f14>
>>> a.m
<method A.m of B instance at 100e3f14>
>>> getattr(a, 'm')
<method A.m of B instance at 100e3f14>
>>>
Or did you mean something else?
More information about the Python-list
mailing list