Proper Way of checking for a collections class

Oren Tirosh oren-py-l at hishome.net
Fri Apr 23 04:02:21 EDT 2004


On Thu, Apr 22, 2004 at 01:23:00PM -0400, Gary Coulbourne wrote:
> Howdy,
> 
> I've been trying to find a good idiom for testing if the input to a
> function is one of the collections classes... the best I could do is this:
> 
> if hasattr(thing_to_test, '__iter__'):
>     ...
> 
> Is this right?  Or, is there a better way besides a cascade of type()
> tests?

Search for "look before you leap" (LBYL) in this newsgroup and you will
find lots of discussions about the issue. The recommended style is not to
LBYL - just try to do whatever you want to do with an object and catch
any resulting exceptions.

  Oren




More information about the Python-list mailing list