[Types-sig] IsInstance

Guido van Rossum guido@CNRI.Reston.VA.US
Tue, 14 Dec 1999 11:45:38 -0500


> From: Paul Prescod <paul@prescod.net>

> I wanted the function to return an object:
> 
> myList=isinstance( foo, types.ListType )
> if not myList:
> 	myDict=isinstance( foo, types.DictionaryType )

Good feature idea, but abusing isinstance() is a bad name.  In C++ I
believe this is called a dynamic cast.  Long ago I learned to define
virtual functions that would return either an X, if the object was an
X, or a null pointer.

Besides, the "if not myList" test could fail if foo happened to be an
empty list.

> Then we can do the inferencing by looking at a single statement. Compare
> it to this:
> 
> if isinstance( foo, types.ListType ):
> 	myList=foo
> elif isinstance( foo, types.DictionaryType ):
> 	myDict=foo
> 
> That inferencing is just too hard.

Are you sure?

> It isn't a proper cast operator
> anymore. If you are willing to change isinstance to return the object if
> it matches then I would like to use it.

No, call it something else.

--Guido van Rossum (home page: http://www.python.org/~guido/)