I'm reading a Python book and it always speaks about "given X its type is Y", so I'm getting confused.
Is there any difference between asking the type of an object and the class of membership of an object in python2 and python3?
I mean, are type and class two different concepts?
This question comes from the fact that in pythons previous to the 2.1 there was a difference between calling x.__class__
and type(x)
.
A data type is a special kind of classifier, similar to a class. It differs from a class in that instances of a data type are identified only by their value. All copies of an instance of a data type and any instances of that data type with the same value are considered to be equal instances.
An array is a type. Therefore, every type is also either a class (including an enum constant), an interface, a primitive, or an array. There are two distinct categories of types: primitive types and reference types: A variable of primitive type always holds a primitive value of that same type.
A Simple (basic) Class [Also Called – Instance Class, Concrete Class, Complete Class] So, a simple class has methods and their implementation. This class can be instantiated to create object(s) and used to perform an action on the data of the class using its methods. This class can be used for inheritance.
The class is a template for an object but concrete object have a type. Actually, every expression has a type! Here is a good simplification of class and type definitions: A type summarizes the common features of a set of objects with the same characteristics.
Prior to Python 2.2, there was a difference between "[classic] classes" and "[built-in] types."
Basically, you couldn't subclass built-in types and they had other idiosyncratic behavior that made them different from user-defined classes.
This blog post from Python creator Guido van Rossum explains some of the motivation for the unification of user-defined classes and built-in types under the framework of "new-style classes."
The Python wiki also has some good background on the practical differences between the classic and new-style user-defined classes.
tl;dr: In Python 2.7 you can still create a "classic class," for backwards compatibility, but the built-in types behave as new-style classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With