[Tutor] Test for type(object) == ???

Ben Finney ben+python at benfinney.id.au
Fri Feb 10 21:00:11 EST 2017


boB Stepp <robertvstepp at gmail.com> writes:

> I was playing around with type() tonight.  If I type (pun intended), I get:
>
> py3: type(5)
> <class 'int'>

Ceci n'est pas un ‘int’.

> So I naively thought a test for type int should go like:
>
> py3: type(5) == "<class 'int'>"
> False
>
> Hmm.

The output from the REPL can only be text. But a type, or any other
object in memory, is not text on the screen; the text only *represents*
an object.

More than that: You can't type objects into your program code. You have
to *reference* them, often by using names.

So the type ‘int’ can be referenced by the name, ‘int’:

    >>> type(5) is int
    True

And that type, when asked for a text representation, will give you some
text.

    >>> type(5)
    <class 'int'>

The object is not its name. The object is not its representation. And
those can (and in this case, are) all different.

<URL:https://en.wikipedia.org/wiki/The_Treachery_of_Images>

-- 
 \              “Programs must be written for people to read, and only |
  `\        incidentally for machines to execute.” —Abelson & Sussman, |
_o__)              _Structure and Interpretation of Computer Programs_ |
Ben Finney



More information about the Tutor mailing list