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

Jeff Shannon jeff at ccvcorp.com
Wed Aug 22 18:34:30 EDT 2001


Joseph Andrew Knapka wrote:

> Emile van Sebille wrote:
> >
> > Does this help get you started?
> >
> > >>> class A:
> > ...     def a(self):
> > ...             print 'A.a()'
> > ...
> > >>> class B(A):
> > ...     pass
> > ...
> > >>> eval('B.%s' % 'a')
> > <unbound method A.a>
>
> Yes, this is quite nice, thanks :-)

Or, to avoid that eval() ....

>>> class A:
...  def Spam(self):
...   pass
...
>>> class B(A):
...  pass
...
>>> if hasattr(B, 'Spam'):
...  print "Got it!"
...
Got it!

You can use hasattr() on class objects just as easily as on instance
objects.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list