overriding methods - two questions

Donn Ingle donn.ingle at gmail.com
Sat Nov 17 05:56:22 CET 2007


> I am curious as to why you want to go through such contortions.  What
> do you gain. 
for obj in list:
 if obj has a foo() method: 
  a = something
  b = figureitout ( )
  object.foo ( a, b )

I am accepting objects of any class on a stack. Depending on their nature I
want to call certain methods within them. They can provide these methods or
not. 

> What happens, for example, if a subclass of Judy is 
> passed in that does not override foo?  Should foo be called in that
> case or not?
No.

Bruno has given me a good solution:

for obj in list:
 if 'foo' in obj.__class__.__dict__:
  etc.

Although I am concerned that it's a loop ('in') and so may be slower than
some other way to detect foo().

So, that's the story.

/d




More information about the Python-list mailing list