Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When trying the following script in Python 2,

a = 200  
print type(a)   

its output is

<type 'int'>

and in Python 3, the script

a = 200
print (type(a))

its output is,

<class 'int'> 

What may be the reason for that?

like image 980
Nitheesh MN Avatar asked Jan 06 '23 15:01

Nitheesh MN


2 Answers

In the days of yore, built-in types such as int and dict and list were very different from types built with class. You could not subclass built-in types for example.

Gradually, in successive Python 2.x releases, the differences between class types and builtin types have eroded; the introduction of new-style classes (inheriting from object) in Python 2.2 was one such (major) step. See Unifying types and classes in Python 2.2.

Removing the use of type in built-in type representations is just the last step in that process. There was no reason anymore to use the name type anymore.

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

like image 189
Martijn Pieters Avatar answered Jan 16 '23 20:01

Martijn Pieters


type isn't behaving any differently. They just changed things so all classes show up as <class ...> instead of <type ...>, regardless of whether the class is a built-in type like int or a class created with the class statement. It's one of the final steps of the elimination of the class/type distinction, a process that began back in 2.2.

like image 40
user2357112 supports Monica Avatar answered Jan 16 '23 21:01

user2357112 supports Monica