Lists and Tuples

Roy Smith roy at panix.com
Fri Dec 5 11:18:00 EST 2003


I just stumbled upon an interesting tuple/list dichotomy.

The new isinstance() function can take a tuple (but not a list) as its 
second argument.  Why?  Logically, it should take any sequence.  The 
operation it's doing is essentially:

for aType in sequence:
   if isinstance (thing, aType):
      return True
return False

I don't see any reason it should reject a list, but it does:

>>> isinstance (1, [str, int])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes 
and types

This is documented, but it still seems strange.  Why go out of your way 
to reject a list when a list is really a perfectly reasonable thing to 
pass in that context?

Same deal with issubclass().




More information about the Python-list mailing list