Skip to main content

Python type() Function

The type() function either returns the type of the object or returns a new type object based on the arguments passed.

Syntax that returns the type of the specified object (1 parameter)

type(object)

type() Parameters

Python type() function parameters:

ParameterConditionDescription
objectRequiredAny object (number, string, list, custom object etc.)

type() Return Value

Python type() function returns the type of the given object.

Syntax that returns a new type object (3 parameters)

type(name, bases, dict)

type() Parameters

Python type() function parameters:

ParameterConditionDescription
nameRequiredSpecifies the class name
basesOptionalSpecifies the base classes
dictOptionalSpecifies the namespace containing definitions for the class

type() Return Value

Python type() function returns a new type object, effectively creating a new class.

Examples

Example 1: Determining the Type of an Object

x =  10
print(type(x)) # Output:

x = 'Hello'
print(type(x)) # Output: <class 'str'>

x = ['red', 'green', 'blue', 'yellow']
print(type(x)) # Output: <class 'list'>

x = {'name': 'Tom', 'age': 25}
print(type(x)) # Output: <class 'dict'>

output

<class 'int'>
<class 'str'>
<class 'list'>
<class 'dict'>

It works also with Custom Objects:

class MyClass:
num = 0

myObj = MyClass()
print(type(myObj)) # Output: <class '__main__.MyClass'>

output

<class '__main__.MyClass'>
note

If you need to check the type of an object, it is better to use the Python isinstance() function instead. It is because the isinstance() function also checks if the given object is an instance of the subclass.

Example 2: Using type() with Conditional Statements

This example shows how to use the type() function in conditional statements to check if an object is of a specific type.

print(type([]) is list)
print(type([]) is not list)
print(type(()) is tuple)
print(type({}) is dict)
print(type({}) is not list)

output

True
False
True
True
True

Example 3: Using type() to Dynamically Create Classes (type() with 3 parameters)

The type() function can dynamically create classes in Python.

With three arguments, this function returns a new type object. For example, the following two statements create identical type objects.

# The usual way
class X:
a = 1

# With type function
X = type('X', (object,), dict(a=1))

In this example, the type() is used to create a new class named X:

  • The first argument X is the name of the new class.
  • The second argument (object,) specifies that the new class should inherit from the object class, which is the base class for all classes in Python.
  • The third argument dict(a='Foo', b=123) is a dictionary that defines the attributes of the new class.
object1 = type('X', (object,), dict(a='Foo', b=123))
print(type(o1))
print(vars(o1))

output

<class 'type'>
{'a': 'Foo', 'b': 123, '__module__': '__main__', '__dict__': <attribute '__dict__' of 'X' objects>, '__weakref__': <attribute '__weakref__' of 'X' objects>, '__doc__': None}

In this example,

  • A class test is defined with two class attributes a and b.
  • Then, type() is used again to create a new class named Y, which inherits from MyClass.
  • The dictionary dict(a='Foo', b=12) defines the attributes of the new class, overriding the b attribute from the MyClass class.
class MyClass:
a = 'MyClass'
b = 123

object2 = type('Y', (MyClass,), dict(a='MyClass', b=12))
print(type(object2))
print(vars(object2))

output

<class 'type'>
{'a': 'MyClass', 'b': 12, '__module__': '__main__', '__doc__': None}