Updated April 15, 2023
Introduction to Python Type Function
In Python, every value in the Python program has a data type as everything is an object in the program, whereas data types are classes, and variables are the object of these classes. Examples of data types are int, float, string, tuple, etc. To determine what type of object declared in the program, there is a built-in function in Python known as type(). The function type(), when used on any object, returns what type of the specified object it is. This type() method is usually used for debugging of the program. There are two types of parameters passed to these type() function one is with a single parameter, and the second is with three parameters.
How Python type() Function Work?
There are many built-in functions in Python, whereas we declare variables without specifying their data type before it as we do in other languages. So there is a function or method to determine the data type of the variable or object to make it easier for programming. The type() function has two types of parameters single and three parameters. If a single argument is passed, it gives the object, whereas for three arguments are passed, it returns a new type object.
Syntax:
type( object)
type( name, bases, dict)
Parameters in Python Type Function
- object: A single parameter to the type() function returns only the specified object type.
- name: It has the __name__ attribute of the given name of the class.
- bases: It also has __bases__ attribute, which returns the tuple of classes from which the current class is driven
- dict: This has namespaces for the class which returns a dictionary and have the attribute __dict__.
Examples of Python Type Function
The following are the examples of Python Type Function:
Example #1
The example below is code to determine the type of the given variable ‘n’.
Code:
n = [1, 2]
print(type(n))
Output:
Example #2
The below example gives you all the data types with the output:
Code:
n = 2
print(type(n))
f = 2.9
print(type(f))
a = 'educab'
print(type(a))
d = {1: 'one', 2: 'two'}
print(type(d))
t = ('apple', 'banana', 'cherry')
print(type(t))
Output:
- In the above example, we have taken a number that contains value as 2, and in return, it prints the type of the variable ‘n’, which is ‘int’.
- In the second case, we have taken a float number that contains the value 2.9, and it returns the type by printing the type of variable ‘f’ as the float.
- In the third case, we have taken a string ‘educab’, and it returns the type of the variable ‘a’ as string ‘str’.
- In the fourth case, we have taken dictionary type, which contains some value, and it returns the type by printing the type of the variable ‘d’, which is dictionary type ‘dict’.
- In the fifth case, we have taken tuple type with the variable‘t’, which contains some value, and it returns the type as ‘tuple’.
Example #3
Now as type() function has two different types of arguments, will now see type() function with 3 parameters.
Code:
new = type('New', (object, ), dict(var1 = 'Educab', b = 2020))
print(type(new))
print(vars(new))
Output:
Example #4
Another example that will clearly show you the use type() function with three arguments.
Code:
t1 = type('A', (object,), dict(p='Foo', x=43))
print(type(t1))
print(vars(t1))
class test:
p = 'Foo'
x = 12
t2 = type('B', (test,), dict(p='Foo', x=12))
print(type(t2))
print(vars(t2))
Output:
- In the above program, we have taken type() function with three parameters in this program we have used vars() function, in Python vars() function returns dictionaries attribute like __dict__attribut. This dictionary is used to store the object’s writable attribute in the __dict__ attribute.
- The type() function has two different types of stating arguments, one with identifying the type of the object in a single parameter and the other has three arguments that will give us the tuple with name, type of the variable and in the dictionary form. So using this built-in function helps to debug the programs to know the correct data types of the initialized variable. Sometimes this function may not be used on string data type, but in very few cases which yields some errors which will be very difficult to handle such errors so to avoid such difficulties, we can use the type() function, and as the requirement, we can convert the given string into a required string to execute the program with error-free.
- Another use of this type() function in which we can register the tables in the database using SQL, and we can also dynamically initialize classes along with their attributes.
Conclusion
In Python, there are many data types that you can determine using a built-in function of Python known as type(). In other programming languages, we declare data type before declaring or initializing the variable, but in Python, we don’t declare any data type; we just write the variable with its values it takes the data type of its own. If we want to know the data type of the variable, we can use this built-in function. This function is useful when we have confusion about the data type declared in other programs, and this function is also useful while debugging any program.
Recommended Articles
We hope that this EDUCBA information on “Python Type Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.