Type () function in Python
The built-in function type () in Python returns the type of object passed as parameter. The type () function is mainly used for debugging purposes.
Syntax
There are two different types of parameters that can be passed to the type () function, which are called single parameters and three parameters.
type(object)
type(name, bases, dict)
- If the type of single parameter (object) is passed, the result will return the given object type.
- If the third parameter (name, bases, dict) is passed, it returns a new type object.
type () with a single object parameter (single parameter)
If the type of single parameter (object) is passed, the result will return the given object type.
Example: How to get the object type?
numberList = [1, 2]
print(type(numberList))
numberDict = {1: 'one', 2: 'two'}
print(type(numberDict))
class Foo:
a = 0
InstanceOfFoo = Foo()
print(type(InstanceOfFoo))
Run the program, the result is:
If you want to test the object type, you can also use the Python function isinstance (), because isinstance () function is also responsible for checking whether the given object is an instance of the subclass.
type () with three parameters name, bases, dict (parameter three)
If the third parameter (name, bases, dict) is passed, it returns a new type object.
name
: class name will become attribute __name__.bases
: tuple of classes, corresponding to the __base__ attribute.dict
: a dictionary contains namespaces for the class, corresponding to the __dict__ attribute.
Example: Create object type using type ()
o1 = type('X', (object,), dict(a='Foo', b=12))
print(type(o1))
print(vars(o1))
class test:
a = 'Foo'
b = 12
o2 = type('Y', (test,), dict(a='Foo', b=12))
print(type(o2))
print(vars(o2))
Run the program, the result is:
{'a': 'Foo', 'b': 12, '__weakref__': objects>, '__dict__': , '__module__':
'__main__', '__doc__': None}{'a': 'Foo', 'b': 12, '__weakref__': objects>, '__dict__': , '__module__':
'__main__', '__doc__': None}{'a': 'Foo', 'b': 12, '__weakref__': objects>, '__dict__': , '__module__':
'__main__', '__doc__': None}
{'a': 'Foo', '__module__': '__main__', 'b': 12, '__doc__': None}
In the above example, the vars () function is used to return the __dict__ attribute . __dict__ is used to store writable properties of the object.
See also: Built-in Python functions
You should read it
May be interested
- The map () function in Pythoncontinuing with the topic of built-in functions in python, the article will introduce you to the map () function with syntax, usage as well as specific examples. invites you to read the track.
- The function dir () in Pythonthe dir () function in python returns a list of valid properties of the object. quantrimang will learn more about this function content through the article. invites you to read the track.
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.