In this tutorial, we will learn about the Python type[] function with the help of examples.
The type[]
function either returns the type of the object or returns a new type object based on the arguments passed.
Example
prime_numbers = [2, 3, 5, 7]
# check type of prime_numbers
result = type[prime_numbers]
print[result]
# Output:
type[] Syntax
The type[]
function has two different forms:
# type with single parameter type[object] # type with 3 parameters type[name, bases, dict]
type[] Parameters
The type[]
function either takes a single object
parameter.
Or, it takes 3 parameters
- name - a class name; becomes the __name__ attribute
- bases - a tuple that itemizes the base class; becomes the
__bases__
attribute - dict - a dictionary which is the namespace containing definitions for the class body; becomes the
__dict__
attribute
type[] Return Value
The type[]
function returns
- type of the object, if only one object parameter is passed
- a new type, if 3 parameters passed
Example 1: type[] with Object parameter
numbers_list = [1, 2]
print[type[numbers_list]]
numbers_dict = {1: 'one', 2: 'two'}
print[type[numbers_dict]]
class Foo:
a = 0
foo = Foo[]
print[type[foo]]
Output
If you need to check the type of an object, it is better to use the Python isinstance[] function instead. It's because the isinstance[]
function also checks if the given object is an instance of the
subclass.
Example 2: type[] With 3 Parameters
o1 = type['X', [object,], dict[a='Foo', b=12]]
print[type[o1]]
print[vars[o1]]
class test:
a = 'Foo'
b = 12
o2 = type['Y', [test,], dict[a='Foo', b=12]]
print[type[o2]]
print[vars[o2]]
Output
{'a': 'Foo', 'b': 12, '__module__': '__main__', '__dict__': , '__weakref__': , '__doc__': None} {'a': 'Foo', 'b': 12, '__module__': '__main__', '__doc__': None}
In the program, we have used the Python vars[] function which returns the __dict__
attribute. __dict__
is used to store object's writable attributes.
You can easily change these attributes if necessary. For example, if you need to change the __name__
attribute of o1 to
'Z'
, use:
o1.__name = 'Z'
type[] method returns class type of the argument[object] passed as parameter in Python.
Syntax of type[] function
Syntax: type[object, bases, dict]
Parameters :
- object: Required. If only one parameter is specified, the type[] function returns the type of this object
- bases : tuple of classes from which the current class derives. Later corresponds to the __bases__ attribute.
- dict : a dictionary that holds the namespaces for the class. Later corresponds to the __dict__ attribute.
Return: returns a new type class or essentially a metaclass.
Example 1: type[] with Object parameter
Here we are checking the object type using the type[] function.
Python3
a
=
[
"Geeks"
,
"for"
,
"Geeks"
]
b
=
[
"Geeks"
,
"for"
,
"Geeks"
]
c
=
{
"Geeks"
:
1
,
"for"
:
2
,
"Geeks"
:
3
}
d
=
"Hello World"
e
=
10.23
f
=
11.22
print
[
type
[a]]
print
[
type
[b]]
print
[
type
[c]]
print
[
type
[d]]
print
[
type
[e]]
print
[
type
[f]]
Output:
What is a type[] function in Python?
The type[] function is mostly used for debugging purposes. Two different types of arguments can be passed to type[] function, single and three arguments. If a single argument type[obj] is passed, it returns the type of the given object. If three arguments type[object, bases, dict] is passed, it returns a new type object.
Applications:
- type[] function is basically used for debugging purposes. When using other string functions like .upper[], .lower[], .split[] with text extracted from a web crawler, it might not work because they might be of different type which doesn’t support string functions. And as a result, it will keep throwing errors, which are very difficult to debug [Consider the error as GeneratorType has no attribute lower[] ].
- type[] function can be used at that point to determine the type of text extracted and then change it to other forms of string before we use string functions or any other operations on it.
- type[] with three arguments can be used to dynamically initialize classes or existing classes with attributes. It is also used to register database tables with SQL.
Example 2: Check object parameter
In this example, we are testing the object using conditions, and printing the boolean.
Python3
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: Use of type[name, bases, dict]
Here, print type[] function which returns class ‘type’.
Python3
new
=
type
[
'New'
, [
object
, ],
dict
[var1
=
'GeeksforGeeks'
, b
=
2009
]]
print
[
type
[new]]
print
[
vars
[new]]
class
test:
a
=
"Geeksforgeeks"
b
=
2009
newer
=
type
[
'Newer'
, [test, ],
dict
[a
=
'Geeks'
, b
=
2018
]]
print
[
type
[newer]]
print
[
vars
[newer]]
Output :
{'__module__': '__main__', 'var1': 'GeeksforGeeks', '__weakref__': , 'b': 2009, '__dict__': , '__doc__': None} {'b': 2018, '__doc__': None, '__module__': '__main__', 'a': 'Geeks'}