I just recently started to teach myself how to code. I am currently reading Think Python 2 for python 3 and when it teaches about the type()
function, it gives the example type(2)
which outputs <class 'int'>
. It then states that "the word 'class' is used in the sense of a category; a type is a category of values."
The part that confuses me is that the type()
function outputs class instead of type. Also, I'm not sure about the difference between type and class; are string, float point, and integer classes of the type "value", or are they the same thing?
I have looked this up but cannot find an answer to my specific questions or simple enough for me to understand.
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 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.
Remember that, in Python, everything is an object. Classes are objects as well. As a result, a class must have a type.
Python has a lot of built-in functions. The type() function is used to get the type of an object. When a single argument is passed to the type() function, it returns the type of the object. Its value is the same as the object.
Python Classes and Objects. A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it ...
I am currently reading Think Python 2for python 3 and when it teaches about the type()function, it gives the example type(2)which outputs <class 'int'>. It then states that "the word 'class' is used in the sense of a category; a type is a category of values." The part that confuses me is that the type()function outputs class instead of type.
I am currently reading Think Python 2for python 3 and when it teaches about the type()function, it gives the example type(2)which outputs <class 'int'>. It then states that "the word 'class' is used in the sense of a category; a type is a category of values."
The python hierarchy is Type (Metaclass) -> Class -> Instance. Think of the function type () as going one level up. If you use the function type () on an instance of an int (any integer) like so: type (123) you will receive the class of the instance which in this case is int.
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.
This difference was artificial, a limitation in the language implementation. Starting with Python 2.2, the developers of Python have slowly moved towards unifying the two concepts, with the difference all but gone in Python 3. Built-in types are now also labelled classes, and you can extend them at will.
Your book is trying to explain a difference that isn't present in Python anymore. Even in Python 2 the difference is only there in name, since type(2)
shows the word 'type' is still used there:
>>> type(2) <type 'int'>
but you can subclass int
just like any other class.
(Python 2 does still have old-style classes, those that don't inherit from object
; these are a remnant of the old system from before the unification.)
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