Type/Class Distinction

Michael Hudson mwh at python.net
Sun Jun 17 05:12:29 EDT 2001


m.faassen at vet.uu.nl (Martijn Faassen) writes:

> Alex Martelli <aleaxit at yahoo.com> wrote:
> > "Martijn Faassen" <m.faassen at vet.uu.nl> wrote in message
> > news:9gdpbp$61k$6 at newshost.accu.uu.nl...
> >    ...
> >> Right now we have to *treat* 'types' differently from 'classes'. For
> >> built-in objects you have to use the type() construct to check what
> >> you're dealing with, but for class instances you use isinstance().
> >>
> >> Wouldn't it be nice if the language allowed something like this:
> >>
> >> if isinstance(foo, int):
> 
> > You're behind the times, I think (if I understand you correctly):
> 
> So I am! I wasn't aware this was the case. Does not seem to be mentioned in
> Andrew Kuchling's 'What's new in Python 2.1'. 

Well, that would be because it wasn't "new in Python 2.1"; 

    >>> import types
    >>> isinstance(1, types.IntType)
    1

works in 1.5.2 (the oldest version I have lying around).

I found:

-----------------
revision 2.112
date: 1997/12/02 19:11:45;  author: guido;  state: Exp;  lines: +15 -10
Support type objects in isinstance().

E.g. isinstance('',types.StringType) will return true now instead of
raising a TypeError exception.  This is for JPython compatibility.
-----------------

in the CVS logs for Python/bltinmodule.c, which seems to be just
before Python 1.5 came out.

Cheers,
M.

-- 
 The rapid establishment of social ties, even of a fleeting nature,
 advance not only that goal but its standing in the uberconscious
 mesh of communal psychic, subjective, and algorithmic interbeing.
 But I fear I'm restating the obvious.  -- Will Ware, comp.lang.python



More information about the Python-list mailing list