[Tutor] Test for type(object) == ???
eryk sun
eryksun at gmail.com
Fri Feb 10 21:05:30 EST 2017
On Sat, Feb 11, 2017 at 1:34 AM, boB Stepp <robertvstepp at gmail.com> wrote:
> I was playing around with type() tonight. If I type (pun intended), I get:
>
> py3: type(5)
> <class 'int'>
`type` is a metaclass that either creates a new class (given 3
arguments: name, bases, and dict) or returns a reference to the class
of an existing object. type(5) is the latter case, and it returns a
reference to the class `int`. What you see printed in the REPL is
repr(int), a string representation of the class object:
>>> repr(int)
"<class 'int'>"
Speaking of classes and metaclasses, note that you can't call
int.__repr__(int) to get this representation, because the __repr__
special method of int is meant for instances of int such as int(5).
Instead you have to explicitly use __repr__ from the metaclass, i.e.
`type`:
>>> type(int)
<class 'type'>
>>> type.__repr__(int)
"<class 'int'>"
But just use built-in repr().
More information about the Tutor
mailing list