Python / type() / Check a Value’s Data Type

Python’s type(value) returns the value’s actual type object, not a string such as "int". For an exact type check, compare it with type(value) is int. Use isinstance() for ordinary checks that should also accept subclasses.

Basic use and the returned object

With one argument, type() returns that object’s type. Types themselves are objects, just like numbers, strings, and collections. An interactive shell displays expression results, but a script needs print() or another display mechanism.

These examples run with Python 3.12. Save each complete example in UTF-8 as type_example.py and run python type_example.py, or python3 where appropriate. Replace the contents to run examples independently.

values = (123, 12.3, "abc", [1, 2], (1, 2), {"a": 1}, {1, 2}, 1 + 2j, True, None)
for value in values:
    print(type(value))

Expected output:

<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'set'>
<class 'complex'>
<class 'bool'>
<class 'NoneType'>

<class 'int'> is the displayed representation of the integer type object. Comparing it to a string does not check the type. Use __name__ for a readable label, but compare type objects for decisions: unrelated classes can share the same name.

kind = type(123)
print(kind is int)
print(kind == "int")
print(kind.__name__)
True
False
int

The three-argument form type(name, bases, dict) creates a new class. Distinguish that separate purpose from the one-argument inspection form.

Exact checks versus isinstance()

type(value) is T checks that the actual type is precisely T. isinstance(value, T) also accepts subclasses, making it useful with inheritance. This complete example defines Dog as a subclass of Animal and compares numeric types too.

class Animal:
    pass

class Dog(Animal):
    pass

pet = Dog()
print(type(pet).__name__)
print(type(pet) is Animal)
print(isinstance(pet, Animal))
print(type(True) is int)
print(isinstance(True, int))
print(isinstance(3, (int, float)))
Dog
False
True
False
True
True

A Dog has actual type Dog, rather than Animal, but is an Animal instance. Similarly, bool is a subclass of int, so isinstance(True, int) is true. If quantities must reject Boolean values, require an exact int or exclude bool separately. A tuple of types, as shown, accepts multiple alternatives.

Reassigning a variable to another type

When reasoning about Python objects and types, distinguish a name from the object it currently references. Assigning a string to a name does not turn the previous integer object into a string.

value = 123
original = value
print(type(value).__name__)
value = "123"
print(type(value).__name__)
print(type(original).__name__)
int
str
int

Initially both names refer to the same integer. After value = "123", value refers to a string while original still refers to the integer. Rebinding differs from converting a value through int() or str().

Container types, contents, and type hints

type(values) does not verify the types of every list element. A type hint such as list[int] does not tell the Python runtime to validate or convert those elements automatically.

values: list[int] = [1, "2"]
print(type(values).__name__)
print(all(type(item) is int for item in values))
try:
    print(isinstance(values, list[int]))
except TypeError:
    print("TypeError")
list
False
TypeError

A list containing the string "2" can exist at runtime despite its int annotation. The per-element exact-int check is false. Passing list[int] as the second argument of isinstance() raises TypeError. Separate the container check from its internal schema, and also use warnings from a static type checker.

Check None and the value’s conditions

None is a distinct object representing absence; value is None checks it clearly. Passing a type check does not guarantee an application-valid value. This function accepts non-negative exact integers and excludes bool.

value = None
print(value is None)
print(type(value).__name__)

def valid_count(value):
    return type(value) is int and value >= 0

for value in (3, -1, True, "3", None):
    print(repr(value), valid_count(value))
True
NoneType
3 True
-1 False
True False
'3' False
None False

and skips the comparison when the type check is false, avoiding >= 0 on a string or None. In contrast, -1 is an integer but fails the value condition. Decide whether input should be converted or restricted to particular types, then add range, missing-value, and format requirements.