sufficiently pythonic code for testing type of function

Bruno Desthuilliers onurb at xiludom.gro
Wed Oct 11 13:09:18 CEST 2006


Theerasak Photha wrote:
> On 10/11/06, Fredrik Lundh <fredrik at pythonware.com> wrote:
> 
>> can be a lot better than a 30-level traceback that ends with a line
>> looking something like
>>
>>      fnut.index(gah)
> 
> Despite long experience with Perl, I am not a big follower of the
> "goose_level: blah" method of error reporting...
> 
>> also, checking for __method__ is, in general, not the right way to check
>> if an object implements a given interface.
> 
> I had a vague feeling it might not be a Right Thing(tm).
> <flame-proof-underwear>I kind of miss
> responds_to?</flame-proof-underwear>

getattr(obj, name[,default]) is your friend. Remember that methods are
just callable attributes.

>> so if you can, try doing the operation instead, and catch error (if you
>> really need to check first, use an idempotent operation, if available).
> 
> I'll do that as soon as my green noggin figures out what 'idempotent'
> means.

idempotent -> no side effects.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list