Type in Python | type() function in Python
Learn via video course
Introduction
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
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() function 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() function 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.
What is the type function in Python?
The type() function is an in-built function in Python. It returns the class type of the object passed to it and is used for debugging purposes in most cases.
Two different types of arguments can be passed to the type() function (single and triple arguments). If a single argument (an object) is passed to the type() function, the function returns the class type of the object, and if three arguments (name, bases, dict) are passed to the function, the function will return 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.
Real-Life Usage of the type() function
The type() function in Python is basically used for debugging purposes. For instance, when we use the string methods like .upper(), .lower(), .casefold(), or .split() with text extracted from a web crawler (a bot, that downloads and indexes content from all over the internet), the program may or may not work because the text extracted might be of a different data type which doesn't support string methods. The program will keep throwing errors, the type() function can be very useful at that point to determine the type of data of the extracted text before using the text with string methods to avoid any error.
We can also use the type() function with three arguments (name, bases, dict) in Python for dynamically initializing classes or existing classes with attributes.
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'>.
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: