89DEVs

Python type() function

overview of type()

The type() function returns the type of the given object. If no object is passed, it returns a new type object based on three arguments name, bases, and dict.

use of type()

The most basic use of the type() function is to determine the type of an object. # use of type() print(type('Python')) print(type([1, 2, 3])) print(type((1, 2, 3))) print(type(1.22)) print(type(1)) The type() function is used to return the type of each object. <class 'str'> <class 'list'> <class 'tuple'> <class 'float'> <class 'int'>

syntax of type()

If an object is passed, the syntax of the type() function is: type(object) If no object is passed, the syntax of the type() function is: type(name, bases, dict)

arguments of type()

The type() function accepts either one argument, which has to be an object or three arguments. The arguments of type(), if an object is passed:
argument required description
object required the object, whose type to be returned
The arguments of type(), if no object is passed:
argument required description
name required the class name, it becomes the __name__attribute
bases required a tuple to itemize the base class, it becomes the __bases__ attribute
dict required a dictionary containing definitions of the body, it becomes the __dict__ attribute

return values of type()

If an object is passed to the type() function it returns the type of the object. If no object is passed, a new type object is returned based on the three arguments.

                
        

Summary


Click to jump to section