[Python-Dev] Evil isinstance()

Aahz aahz@pythoncraft.com
Sat, 30 Mar 2002 09:45:19 -0500


[We had a discussion about this a couple of months ago on c.l.py, which
Guido probably didn't see, so I wanted to briefly raise this issue here.]

On Sat, Mar 30, 2002, Guido van Rossum wrote:
> David Abrahams:
>> 
>> Given that Bool is immutable, I have no cases other than examples of
>> type introspection which can be written to account for the fact that
>> Bool is-a Int. However, it does make certain things trickier to get
>> right:
>> 
>> numeric_types = [ Int, Long, Bool, Float, Complex ]
>> for t in numeric_types:
>>     if isinstance(x, t):
>>         # Do something...
>> 
>> This sort of thing could depend on getting the order of the list right
>> (I didn't).
> 
> Using isinstance() this way is usually bad.  And who knows that long
> doesn't inherit from int?  (At some point in the future it may, or
> they may become the same thing -- see PEP 237.

The problem is that in some cases the __init__ for a class needs to
dispatch based on the type of the operand.  For example, int() takes
both numbers and strings.  What should we recommend as "standard
practice" for this issue if isinstance() is disrecommended?
-- 
Aahz (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?