Difference of type () function in Python 2 and Python 3

When trying to use a script in Python 2

a = 200 print type(a) 

his way out

 <type 'int'> 

and in Python 3, a script

 a = 200 print (type(a)) 

his way out

 <class 'int'> 

What is the reason for this?

+5
source share
2 answers

On days of storage, built-in types such as int and dict and list were very different from types built with class . For example, you cannot subclass built-in types.

Gradually, in successive releases of Python 2.x, the differences between class types and built-in types blur; introducing new-style classes (inheriting from object ) in Python 2.2 was one such (basic) step. See Combining types and classes in Python 2.2 .

Removing the use of type in inline type representations is just the last step in this process. Now it no longer made sense to use the type name.

In other words, between Python 2.7 and 3.x this is a cosmetic change, nothing more.

+7
source

type does not behave differently. They just changed the situation, so all classes are displayed as <class ...> instead of <type ...> , regardless of whether the class is a built-in type of type int or a class created using the class operator. This is one of the final steps to eliminate class / type differences, the process that started in 2.2.

+3
source

All Articles