Python 3 isinstance

Duncan Booth duncan.booth at invalid.invalid
Thu Jan 15 20:08:03 CET 2009


Carl Banks <pavlovevidence at gmail.com> wrote:

> I don't see what the big deal is.  Right now isinstance accepts a type
> or a tuple of types.  The code could be changed to allow a type, or
> any iterable the returns types (wherein every items of the sequence is
> required to be a type).  What's messy about that?

No, it isn't restricted to a type or a tuple of types: right now isinstance 
accepts X where X is a type or a tuple of X. The definition is recursive, 
and the data structure can be deeply nested, but (because tuples are 
immutable) it cannot be infinite.

e.g.
>>> isinstance('x', (((((int, (float, str)),),),),))
True

If you change the code so that it only allows iterables one level deep then 
existing code may break. If you change it to allow mutable nested values 
then the interpreter has to avoid getting stuck in an infinite loop.



More information about the Python-list mailing list