Sometimes, we want to determine the type of an object with Python.
In this article, we’ll look at how to determine the type of an object with Python.
How to determine the type of an object with Python?
To determine the type of an object with Python, we can use the type
function.
For instance, we write:
class Test1 (object):
pass
class Test2 (Test1):
pass
a = Test1()
b = Test2()
print(type(a) is Test1)
print(type(b) is Test2)
We have 2 classes, Test1
and Test2
.
And we create an instance of each and assign it to a
and b
respectively.
Then we call type
function with a
and b
to check if it’s an instance of Test1
and Test2
respectively.
Therefore, we see that both are True
.
Conclusion
To determine the type of an object with Python, we can use the type
function.