On Nov 16, 11:35 am, Donn Ingle <[EMAIL PROTECTED]> wrote:
> >This may help (on an old Python version):
> >>>> class Sam: pass
> >>>> class Judy:
> > ...    def foo(self): pass
> > ...
> >>>> children = [Sam(), Judy(), Sam()]
> >>>> for child in children: hasattr(child, "foo")
> > ...
> > False
> > True
> > False
>
> That's not what my tests are showing. While Sam has no foo, it's coming from
> (in my OP) Child (which is the parent class), so hasattr(Sam(),"foo") is
> returning True.
>
> /d

But also in your OP: "I want to run the foo() method for each one that
*has* a foo() method ...."  So hasattr(child, "foo") really does
answer the question as posed, even if it's not really what you want.
I am curious as to why you want to go through such contortions.  What
do you gain.  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?

--Nathan Davis
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to