Probably a stupid question... name-->method lookup

Christian Tanzer tanzer at swing.co.at
Thu Aug 23 07:55:15 CEST 2001


Joseph Andrew Knapka <jknapka at earthlink.net> wrote:

> I have a method name (string) and a class object. I'd like to
> be able to look up the the named unbound method of the class.
> I know that TheClass.__dict__[<methodname>] will kinda
> work, but it will fail if <methodname> is implemented only in
> a base class of TheClass. I could do the Python method lookup
> dance via __bases__ etc., but I'd be surprised if there weren't
> already a standard function to do it. Sadly, perusal of the
> tutorial, library reference, language reference (briefly),
> and FAQ has thus far been unenlightening. Pointers
> appreciated.

Python 2.1 (#1, May  2 2001, 18:27:26) 
[GCC 2.7.2.1] on linux2
Type "copyright", "credits" or "license" for more information.
>>> class A:
...   def foo(): pass
... 
>>> class B(A): pass
... 
>>> getattr (B, "foo")
<unbound method A.foo>
>>> 

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list