type() function in Python
The type() function is an inbuilt function in Python. It returns the class type of the argument (an object) passed to it, and if three arguments (name, bases, and dict) are passed to it, it returns a new type object. The type() function is mostly used for debugging and dynamically initializing a class.
Example of type() function in Python
This example is to give an idea of the working of the type() function in Python:
Source code:
Output
In the above example, we are finding the data type (since, in Python programming, data types are classes, and variables are instances of these classes) of the variable my_str using the type() function in Python. According to the output, the data type of my_str is 'str' (it belongs to the class 'str').
Syntax of type() in Python
The type() function in Python accepts two different types of arguments (single and three arguments):
Syntax of single argument: A single object is passed to the type() function; type(object)
Syntax of three arguments: Three arguments (name, bases, dict) are passed to the type function; type(name, bases, dict)
Parameters of type() function in Python
The type() function in Python accepts either an object as an argument or "name", "bases" and "dict" as three of its arguments. Let's look at the meaning of these parameters:
- Object: The object whose class type is to be returned.
- Name: A string that is the name of the class. It corresponds to the __name__ attribute of the class.
- Bases: A tuple that presents the individual items of the base class as a list. It corresponds to the __bases__ attribute of the class.
- Dict (dictionary): A dictionary that helps create the class body. It corresponds to the __dict__ attribute of the class.
Return Values of type() in Python
There are two types of return values of the type() function:
- On passing a single object as an argument: The return value of the type() function is in angular brackets as <class 'name of the class'>.
- On passing name, bases, dict as arguments: The return value of type() function is a new type object.
How to Use type() function in Python?
We can use the type() function to return the class type of an object (data type) by passing the object as its argument. We can find out the class type of an object and even confirm whether a value is of a certain data type or not using this.
Let's have a look at an example:
Source code:
Output:
In the above example, we are checking whether the values val1 and val2 are of integer data type. As a result, we get that only val1 is an integer.
We can also use the type() function to return a new type object by passing three arguments (name, bases, and dict) to it. It allows us to create classes dynamically and makes the type() function a type object itself.
The examples will be in the section "Examples of type() function in Python" below.
More Examples of type() function in Python
Below are some examples of using the type() function covering all the cases:
Using the type() function for various objects
Output
In the above example, we are finding the data type of different objects using the type() function.
Using the type() function for a class object
Output
In the above example, we return the class type of the "TestClass" object using the type() function.
Using the type() function for returning a type object
Output
In the above example, we first dynamically create a class using the type() function, returning a type object. We then return the __name__, __bases___, and __dict__ attributes of the class. Then we return the values of the dictionary,i.e. the attributes of the object, and finally, we check the class type of the type object, which comes as <class 'type'>.
Finding the Type of a Python Object
The type() function returns the type of the specified object, helping to understand what kind of data you're working with.
Example:
Output:
Check if an Object is of a Specific Type in Python
You can compare the output of type() with a type object to check if an object is of a specific type.
Example:
Output:
Using type() with Conditional Statement
type() can be used in conditional statements to execute different code based on the data type of an object.
Example:
Output:
type() With 3 Parameters
type() can also be used with three parameters - type(name, bases, dict), to dynamically create a new type (class).
Example:
Output:
This dynamic class creation feature of type() is powerful, allowing for the creation of classes at runtime. Each use case of type() highlighted above showcases its versatility in Python programming, from simple type checks to advanced metaprogramming techniques.
Applications of type() Function in Python
- Dynamic Type Checking: type() function is used to check an object's data type dynamically, enabling flexible code execution paths.
- Implementing Polymorphism: Allows functions to handle arguments of different types in a type-specific manner.
- Debugging Assistance: Helps in identifying variable types during debugging, improving code understanding and error diagnosis.
- Type-Based Dispatch: Enables dispatching behavior based on the type of the arguments, supporting more generic programming patterns.
- Ensuring API Integrity: Verifies that inputs to functions or methods adhere to expected types, safeguarding against type-related errors.
Conclusion
- The type() function is an inbuilt function in Python.
- On passing a single argument (an object) to the type() function, type() returns the object's class type.
- On passing three arguments (name, bases, and dict) to the type() function, type() dynamically creates a class (a type object).
- The type() function is mostly used for debugging purposes.
See Also
If you need some help with this article or want to move forward, refer to these topics: