How to check the type of a variable in python?

In Python, we have a built-in method called type() that helps us to figure out the type of the variable used in the program. We can pass a single or three arguments to the type() function.

Passing a single argument to the type() function

Passing a single argument to type() function i.e type(object) returns the object's type. For example, if the input is a string we will get output as <class 'str'> if the input is an integer we will get output as <class 'int'>.

The syntax for passing a single argument is given below.

#syntax:
type(object)​

Example 1: We have different variables of string, list, integer, etc data type. The variable names are passed as an argument to the type() function to get the type of the variable.

str_1 = "Python"
print(type(str_1))

int_val = 123
print(type(int_val))

float_val = 456.897
print(type(float_val))

cmplx_val = 3 + 4j
print(type(cmplx_val))

list_1 = [1, 2, 3]
print(type(list_1))

tup_1 = (3, 4, 5)
print(type(tup_1))

set_1 = {4, 7, 8}
print(type(set_1))

dict_1 = {'A': 1, 'B': 2, 'C': 3}
print(type(dict_1))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>​
Example 2: Determining the type for a class object
class example:
    pass

obj_1 = example()
print(type(obj_1))​
The type() function returns the class type and the class name as output.
<class '__main__.example'>
Passing three argument to type() function​

Passing three arguments to the type() function

Passing three arguments i.e type(name, bases, dict) returns a new type object. The type() function with three arguments creates a new class dynamically at the run time. The syntax for the type() function with three arguments is given below.

#syntax:
type(name, bases, dict)

Parameters

  • name : It takes a string as a parameter that represents the class name, and becomes __name__ attribute of the class.
  • bases : It takes tuples as an argument that specifies the base class for the main class, and becomes the __base__ attribute of the class.
  • dict : It takes a dictionary as an argument that creates the body of the class specified, and becomes the __dict__attribute of the class.

Consider the following example, we create a dynamic class my_class1 with no base classes and another class my_class2 with a base class. The vars() function returns the dict attribute for a class/module.

# with no base class
var_1 = type('my_class1', (object,), {'A': 'red', 'B': 'black'})
print(type(var_1))
print(vars(var_1))

# with a base class
class base_class:
    a = "python"
    b = "programming"
   
var_2 = type('my_class2', (base_class,),  {'a': 'red', 'b': 'black'})
print(type(var_2))
print(vars(var_2))

Output

<class 'type'>
{'A': 'red', 'B': 'black', '__module__': '__main__', '__dict__': <attribute '__dict__' of 'my_class1' objects>, 
'__weakref__': <attribute '__weakref__' of 'my_class1' objects>, '__doc__': None}
<class 'type'>
{'a': 'red', 'b': 'black', '__module__': '__main__', '__doc__': None}
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions