type()
Tells you the type of any value, such as int, str, list or dict, which helps when a program does not behave the way you expect.
type(object)
type(name, bases, dict, **kwds)With one argument, type() tells you what kind of value something is. type(42) is int, type("42") is str, type([4, 2]) is list. Printed, a type looks like <class 'int'>; its short name is in .__name__.
It is a great debugging tool. When "5" + 3 fails or a total comes out as "53", printing type() of each value shows at once that one of them is text, not a number.
For checks inside your code, prefer isinstance(). type(x) == int is False for True (whose type is bool), and for objects of your own subclasses, while isinstance() treats them as the kind of thing they are. With three arguments, type() does something quite different: it creates a new class. That is an advanced use you will rarely need.
Parameters
| Parameter | What it is |
|---|---|
objectrequired | Any value. type() returns its class. |
name, bases, dictoptional | Advanced: the three-argument form builds a new class with this name, these parent classes (a tuple) and these attributes (a dictionary). |
Return value
The type (class) of the object. With three arguments, a new class.
Examples
1Types of everyday values
for value in [42, 3.5, "hi", True, None, [1], (1,), {"a": 1}, {1}]:
print(repr(value), type(value).__name__)42 int
3.5 float
'hi' str
True bool
None NoneType
[1] list
(1,) tuple
{'a': 1} dict
{1} set2What printing a type looks like
print(type(42))
print(type("Oslo"))
print(type(42) == int)<class 'int'> <class 'str'> True
3Find the bug: text that looks like a number
price = "5"
qty = 3
print(type(price).__name__, type(qty).__name__)
print(price * qty)
print(int(price) * qty)str int 555 15
A string times a number repeats the string. type() shows that price was text all along.
4Division always gives a float
print(type(10 / 2).__name__, 10 / 2)
print(type(10 // 2).__name__, 10 // 2)float 5.0 int 5
/ always gives a float, even when the result is whole. // divides and keeps whole numbers as int.
5type() vs isinstance()
flag = True
print(type(flag) == int)
print(isinstance(flag, int))
print(type(flag).__mro__)False True (<class 'bool'>, <class 'int'>, <class 'object'>)
bool is a subclass of int. type() checks the exact class only; isinstance() also accepts subclasses. __mro__ lists a class and its parents.
Common mistakes
Comparing a type with a string
type(5) == "int" is always False, because a type is not text. Compare with the type itself, or with .__name__ if you really need text.
n = 5
print(type(n) == "int")
print(type(n) == int)
print(type(n).__name__ == "int")False True True
Using type() to check input in functions
if type(x) == float: rejects whole numbers and your own subclasses. isinstance(x, (int, float)) is usually what you mean.
Questions people ask
How do I check the type of a variable in Python?
Print type(x) while exploring. In code that decides something, use isinstance(x, int).
How do I get just the type name as a string?
type(x).__name__ gives 'int', 'str' and so on.
What is NoneType?
The type of None, the value that means "nothing here". There is only one None; check for it with x is None.