Just a simple question : when should I use the term 'class', and when should I use the term 'type' in Python ?
Once upon a time, Python had both types and classes. Types were built-in objects defined in C; classes were what you built when using a class statement. The two were named differently because you couldn't mix these; classes could not extend types.
The class defines object's internal state and the implementation of its operations. In contrast, an object's type only refers to its interface - a set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type.
Remember that, in Python, everything is an object. Classes are objects as well. As a result, a class must have a type.
Python type() is a built-in function that returns the type of the objects/data elements stored in any data type or returns a new type object depending on the arguments passed to the function. The Python type() function prints what type of data structures are used to store the data elements in a program.
It is more or less historical: they used to be different a long time ago, which has no practical implications anymore.
Edit: I use "class
" when referring to concrete implementations and "type
" in a more informal way, when speaking about high level data structures, application arcitecture etc. In my thinking a type is a more general thing, I don't think of every class as a distinct type.
Also, when I use metaclasses (very rarely) I speak of types.
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