Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class vs. Type in Python

Tags:

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.

like image 855
Jeff Avatar asked Mar 12 '16 14:03

Jeff


People also ask

What is the difference between class and 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.

What is the difference between class and type?

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.

Is class A type in Python?

Remember that, in Python, everything is an object. Classes are objects as well. As a result, a class must have a type.

What does type () do in Python?

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.

What are classes and objects in Python?

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 ...

What is the difference between class and type in Python 3?

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.

What is the difference between class and 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."

How do I get the class of an instance in Python?

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.


1 Answers

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.)

like image 185
Martijn Pieters Avatar answered Sep 30 '22 21:09

Martijn Pieters