Python type() Function
The Python type() function returns the type of the specified object, if one argument is provided. With all three arguments, it returns a new type object.
Syntax
type(object, bases, dict)
Parameters
object |
Required. The type() function returns the type of this object, if only one argument is specified. With all three arguments, it becomes __name__ attribute of the class. |
bases |
Optional. Specify the __base__ attribute of the class. |
dict |
Optional. Specify the namespace with the definition for the class. Specify __dict__ attribute of the class. |
Example: type() function with single argument
In the example below, type() function is used to get the type of the specified object.
x = 10 y = [10, 20, 30] z = {'a': 10, 'b': 20} print(type(x)) print(type(y)) print(type(z))
The output of the above code will be:
<class 'int'> <class 'list'> <class 'dict'>
Example: type() function with three argument
In the example below, all three arguments of type() function is used. It returns a new type object.
class rect: length = 10 breadth = 5 obj = type('X', (rect,), dict(length = 10, breadth = 5)) print(obj) print(type(obj)) print(vars(obj))
The output of the above code will be:
<class '__main__.X'> <class 'type'> {'length': 10, 'breadth': 5, '__module__': '__main__', '__doc__': None}
❮ Python Built-in Functions