Testing methods

holger krekel pyth at devel.trillke.net
Sat Apr 19 21:03:06 CEST 2003

Ovid wrote:
> Hello,
> Relatively new to Python and I thought I would teach myself the
> language by building a test harness.  After I'm done, I will probably
> be throwing it away in favor of Pyunit, but it seemed like a good way
> to learn some of the fiddly bits.
> Fiddly bits such as "can this object do that?"  In my test code, I
> want to test whether or not a given class *or* instance of that class
> can execute a particular method (i.e., if it has or inherits the
> method).  For example, one way (in Perl), to test if object "$foo" can
> execute an arbitrary method would be:
>   if ( $foo->can($some_method) )

not knowing much about perl internals what exactly does "can" test? 

Just the existence of a method? Then 

    if hasattr(foo, some_method):

would be the equivalent and is often refered to as 
"look before you leap"-style. 

have fun,


More information about the Python-list mailing list