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

boB Stepp robertvstepp at gmail.com
Fri Feb 10 20:34:35 EST 2017


I was playing around with type() tonight.  If I type (pun intended), I get:

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

So I naively thought a test for type int should go like:

py3: type(5) == "<class 'int'>"
False

Hmm.  So I tried these other failing tests:

py3: type(5) == <class 'int'>
  File "<stdin>", line 1
    type(5) == <class 'int'>
               ^
SyntaxError: invalid syntax

py3: type(5) == 'int()'
False

I finally stumbled onto the correct form:

py3: type(5) == int
True

So my question is why does "type(5)" result in "<class 'int'>", but
the correct Boolean test is "type(5) == int"?  I suspect it has
something to do with the built-in attributes of Python objects that I
currently know so very little about.

As always, many thanks in advance!

-- 
boB


More information about the Tutor mailing list