How to Determine the Type of an Object in Python

Avatar

By squashlabs, Last Updated: November 2, 2023

How to Determine the Type of an Object in Python

In Python, you can determine the type of an object using the type() function. This function returns the type of the object as a string. Here’s an example:

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

In this example, the type() function is used to determine the type of the variable x, which is an integer. The output shows that the type of x is .

You can also use the isinstance() function to check if an object is an instance of a specific class or any of its subclasses. Here’s an example:

class Person:
    pass

class Student(Person):
    pass

person = Person()
student = Student()

print(isinstance(person, Person))    # Output: True
print(isinstance(student, Person))   # Output: True
print(isinstance(person, Student))   # Output: False
print(isinstance(student, Student))  # Output: True

In this example, the isinstance() function is used to check if the objects person and student are instances of the Person and Student classes, respectively. The output shows whether each object is an instance of the specified class.

Using the type() and isinstance() functions, you can easily determine the type of an object in Python and check if it belongs to a specific class or any of its subclasses.

It’s important to note that Python is a dynamically typed language, which means that the type of an object can change during runtime. Therefore, it’s a good practice to check the type of an object before performing any operations or accessing its properties to avoid potential errors.

Alternative Approach: Using the __class__ Attribute

In addition to using the type() function, you can also determine the type of an object in Python by accessing its __class__ attribute. This attribute contains a reference to the object’s class. Here’s an example:

x = 5
print(x.__class__)  # Output: 

In this example, the __class__ attribute is accessed to determine the type of the variable x, which is an integer. The output is similar to using the type() function.

Similarly, you can use the isinstance() function with the __class__ attribute to check if an object is an instance of a specific class or any of its subclasses. Here’s an example:

class Animal:
    pass

class Dog(Animal):
    pass

animal = Animal()
dog = Dog()

print(animal.__class__ is Animal)    # Output: True
print(dog.__class__ is Animal)       # Output: True
print(animal.__class__ is Dog)       # Output: False
print(dog.__class__ is Dog)          # Output: True

In this example, the __class__ attribute is used with the is operator to check if the objects animal and dog are instances of the Animal and Dog classes, respectively. The output shows whether each object is an instance of the specified class.

While the type() function is the recommended way to determine the type of an object in Python, accessing the __class__ attribute can be a useful alternative when needed.

Related Article: How to Use Python Super With Init Methods

Best Practices

When determining the type of an object in Python, it’s important to follow these best practices:

1. Use the type() function or the __class__ attribute to determine the type of an object.
2. Avoid relying solely on the type of an object for decision-making. Instead, use polymorphism and duck typing to write more flexible and reusable code.
3. When checking if an object belongs to a specific class or any of its subclasses, use the isinstance() function.
4. Be aware that the type of an object can change during runtime in dynamically typed languages like Python. Therefore, it’s recommended to check the type before performing any operations or accessing properties to avoid potential errors.
5. Document the expected types of function arguments and return values using type hints to improve code clarity and maintainability.

Related Article: How to Use Class And Instance Variables in Python

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

Python Super Keyword Tutorial

Python's super keyword is a powerful tool that can enhance code functionality. In this tutorial, you will learn how to use the super method to improve your Python code.... read more