

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Type Object
Everything in Python is an object including classes. All classes are instances of a class called "type".The type object is also an instance of type class. You can inspect the inheritance hierarchy of class by examining the __bases__ attribute of a class object. type() method returns class type of the argument(object) passed as parameter.If single argument type(obj) is passed to type method, it returns the type of given object. If three arguments type(name, bases, dict) is passed, it returns a new type object.
Using type()
Let’s look at the classes for the most used data types. In the below program we initialize some variables and then use the type() to ascertain their class.
Example
# Some variables a = 5 b = 5.2 c = 'hello' A = [1,4,7] B = {'k1':'Sun','K2':"Mon",'K3':'Tue'} C = ('Sky','Blue','Vast') print(type(a)) print(type(b)) print(type(c)) print(type(A)) print(type(B)) print(type(C))
Output
Running the above code gives us the following result −
<class 'int'> <class 'float'> <class 'str'> <class 'list'> <class 'dict'> <class 'tuple'>
Type of the classes
If we go deeper to look at the type of classes above, we will see that they are all belong to class named ‘type’.
Example
print(type(int)) print(type(dict)) print(type(list)) print(type(type))
Output
Running the above code gives us the following result −
<class 'type'> <class 'type'> <class 'type'> <class 'type'>
Creating a new Object Type
We can also use a similar approach as above to create new objects. Here we pass three parameters to create the new type object.
Example
Object1 = type('A', (object,), dict(a='Hello', b=5)) print(type(Object1)) print(vars(Object1)) class NewCalss: a = 'Good day!' b = 7 Object2 = type('B', (NewCalss,), dict(a='Hello', b=5)) print(type(Object2)) print(vars(Object2))
Output
Running the above code gives us the following result −
<class 'type'> {'a': 'Hello', 'b': 5, '__module__': '__main__', '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None} <class 'type'> {'a': 'Hello', 'b': 5, '__module__': '__main__', '__doc__': None}
- Related Questions & Answers
- HTML DOM Object type Property
- Determine whether the given object represents a scalar data-type in Python
- What is the difference between dynamic type variables and object type variables?
- Return the scalar dtype or NumPy equivalent of Python type of an object
- Type Conversion in Python
- Convert short primitive type to Short object in Java
- Convert byte primitive type to Byte object in Java
- PHP Casting Variable as Object type in foreach Loop
- Wrap object properties of type string with arrays - JavaScript
- Python object serialization
- Convert the number or boolean type JSON object from string type to its original in JavaScript
- What is a composite data type i.e. object in JavaScript?
- Convert double primitive type to a Double object in Java
- Get the Component Type of an Array Object in Java
- Type and isinstance in Python