[Python-Dev] Evil isinstance()
Guido van Rossum
guido@python.org
Sat, 30 Mar 2002 13:08:55 -0500
> 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?
Ah, bollocks. David was just being annoying, and I was being annoying
back. You can use isinstance() when it makes sense, like your
example.
--Guido van Rossum (home page: http://www.python.org/~guido/)