Python Glossary Русский

all Python 3Data typesfunction

type()

introspection

Built-in function that returns the type of an object. With one argument it returns the type; with three it creates a new class dynamically.

Syntax

type(object) -> type
type(name, bases, dict) -> type

For type checks you usually want isinstance rather than type(x) == C: type() returns the object's exact class and ignores inheritance, so type(True) is bool, not int. The three-argument form is the low-level class constructor (what a metaclass invokes) and rarely appears in ordinary code.

Examples

print(type(42))  # → <class 'int'>
print(type(3.14))  # → <class 'float'>
print(type("hi"))  # → <class 'str'>
print(type(True))  # → <class 'bool'>
print(type(None))  # → <class 'NoneType'>
x = 42
print(type(x) == int)  # → True (сравнение типов)
print(type(True) == type(1))  # → False (bool != int при type())
A = type("A", (), {"x": 42})  # три аргумента — динамическое создание класса
print(A.x)  # → 42

See also

Open in the full glossarydocs.python.orgReport an inaccuracy