In Python, the type function serves as a built-in utility that provides the type of a specified object or can dynamically create a new class.
- When the function is invoked with one argument, it yields the type of that particular object.
- If the function is called with three arguments, it generates a new type of object instead.
The function type is frequently employed in metaprogramming, enabling applications to modify themselves while they are running. By utilizing the type function, a program has the capability to dynamically create new classes and alter those that already exist. This feature is especially beneficial in libraries and frameworks where the creation of new classes is necessary based on user input.
Python type Function Syntax
It has the following syntax:
type(object)
type(name, bases, dict)
Parameters:
It has the following syntax:
object: When a single parameter is provided, the type function returns the type of that object.
name (optional): It is the name of the class.
bases (optional): It specifies the base classes.
dict (optional): It indicates the namespace that contains the class definition.
Returns:
When a single argument is provided to the type function, it yields the type of that particular object. Conversely, if three arguments are supplied, it generates a new type of object.
Different Examples for Python type Function
Python type Function: Single argument
Let us examine an illustration of the type function when a solitary argument is provided:
# Python type() function when single argument is passed
my_word = " C# Tutorial "
print(type(my_word))
Output:
< class 'str'>
Explanation:
In the aforementioned illustration, we have employed the type function to determine the type of the string object named myword. The type function outputs < class ' str '>, indicating that myword is indeed a string object.
Python type Function: Three arguments
Let's examine an illustration of the type function when it receives three arguments:
# Python type() function when three arguments are passed
BaseClass = type(' BaseClass ', (), { 'a' : 100})
print(type(BaseClass))
Output:
< class 'type'>
Explanation:
In the previously mentioned example, we employed the type function to define a new class named BaseClass. We provided three parameters to the type function: the identifier for the new class, BaseClass, an empty tuple indicating the absence of any parent classes, and a variable reference "a" assigned a value of 100. Finally, we displayed the type of the BaseClass instance using the type function, which yields <class 'type' >.
Python type Function Example
The following example demonstrates the method to determine the type of an object.
# Python program for type() function
# List
List = [4, 5]
print(type(List))
# Dictionary
Dict = {4: 'four', 5: 'five'}
print(type(Dict))
# Class
class Python:
a = 0
InstanceOfPython = Python()
print(type(InstanceOfPython))
Output:
<class 'list'>
<class 'dict'>
<class '__main__.Python'>
Explanation:
In the example provided, we have utilized a list object referred to as 'List' that holds various values, and it subsequently outputs the type of the List. In a similar manner, we have employed a dictionary object named 'Dict' that encompasses certain values, which in turn prints the type of the Dict. Furthermore, we have established a class called Python and created an instance named InstanceOfPython, which displays its type.
Conclusion
To summarize, the type function in Python serves as a built-in tool that is employed to determine the type of an object or to dynamically generate a new class. This function can accept either one or three arguments, making it a valuable resource for metaprogramming within the Python ecosystem.