Skip to content
Related Articles

Related Articles

type() function in Python

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 10 Aug, 2022
Improve Article
Save Article

type() method returns class type of the argument(object) passed as parameter in Python.

Syntax of type() function

Syntax: type(object, bases, dict)

Parameters : 

  • object: Required. If only one parameter is specified, the type() function returns the type of this object
  • bases : tuple of classes from which the current class derives. Later corresponds to the __bases__ attribute. 
  • dict : a dictionary that holds the namespaces for the class. Later corresponds to the __dict__ attribute.

Return: returns a new type class or essentially a metaclass.

Example 1: type() with Object parameter

Here we are checking the object type using the type() function.

Python3




a = ("Geeks", "for", "Geeks")
b = ["Geeks", "for", "Geeks"]
c = {"Geeks": 1, "for":2, "Geeks":3}
d = "Hello World"
e = 10.23
f = 11.22
 
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))

Output:

<class 'tuple'>
<class 'list'>
<class 'dict'>
<class 'str'>
<class 'float'>
<class 'float'>

What is a type() function in Python? 

The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three arguments type(object, bases, dict) is passed, it returns a new type object. 

Applications:

  • type() function is basically used for debugging purposes. When using other string functions like .upper(), .lower(), .split() with text extracted from a web crawler, it might not work because they might be of different type which doesn’t support string functions. And as a result, it will keep throwing errors, which are very difficult to debug [Consider the error as GeneratorType has no attribute lower() ]. 
  • type() function can be used at that point to determine the type of text extracted and then change it to other forms of string before we use string functions or any other operations on it.
  • type() with three arguments can be used to dynamically initialize classes or existing classes with attributes. It is also used to register database tables with SQL.

Example 2: Check object parameter

In this example, we are testing the object using conditions, and printing the boolean.

Python3




print(type([]) is list)
 
print(type([]) is not list)
 
print(type(()) is tuple)
 
print(type({}) is dict)
 
print(type({}) is not list)

Output :

True
False
True
True
True

Example 3: Use of type(name, bases, dict) 

Here, print type() function which returns class ‘type’.

Python3




# New class(has no base) class with the
# dynamic class initialization of type()
new = type('New', (object, ),
           dict(var1='GeeksforGeeks', b=2009))
 
# Print type() which returns class 'type'
print(type(new))
print(vars(new))
 
 
# Base class, incorporated
# in our new class
class test:
    a = "Geeksforgeeks"
    b = 2009
 
 
# Dynamically initialize Newer class
# It will derive from the base class test
newer = type('Newer', (test, ),
             dict(a='Geeks', b=2018))
 
print(type(newer))
print(vars(newer))

Output :

{'__module__': '__main__', 'var1': 'GeeksforGeeks', '__weakref__': ,
 'b': 2009, '__dict__': , '__doc__': None}

{'b': 2018, '__doc__': None, '__module__': '__main__', 'a': 'Geeks'}

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!