What is the purpose of subclassing the class "object" in Python?

All Python built-in modules are subclasses of object , and I come across many user-defined classes, which too. What for? What is the purpose of the object class? This is just an empty class, right?

+48
python object deprecated
Apr 6 2018-10-06T00:
source share
6 answers

In short, he sets up free magic ponies.

In the long run, Python 2.2 and earlier used "old-style classes." They were a special implementation of classes, and they had several limitations (for example, you could not create subclass classes). To fix this was to create a new class style. But, this will be due to some changes that are incompatible in the opposite direction. Therefore, to ensure that code written for old-style classes still works, the object class was created to act as a superclass for all new-style classes. So, in Python 2.X, class Foo: pass will create an old style class Foo(object): pass , and class Foo(object): pass will create a new style class.

See Guido Combining Types and Classes in Python 2.2 for longer.

And, in general, it’s nice to get used to all your classes becoming new, because some things (the @property decorator is the one that comes to mind) will not work with the old style classes.

+48
Apr 6 2018-10-10T00:
source share

Short answer: subclassing object effectively makes it a new-style class (note that this is not necessary since it is automatic in Python 3.x)

For the difference between the new style and the old style classes: see this thread stack thread question . For the full story: see this nice post in Python types and objects .

+5
Apr 6 2018-10-06T00:
source share

This is due to the "new style" of classes. You can read more about this here: http://docs.python.org/tutorial/classes.html#multiple-inheritance , as well as here: http://docs.python.org/reference/datamodel.html#new-style -and-classic-classes

Using the new-style classes will allow you to use "new, universal Python functions such as __slots__, descriptors, properties, and __getattribute __ ()."

+4
Apr 6 2018-10-06T00:
source share

Right, but it marks the class as a new-style class. Newly developed classes should use the object base, because it does not require large expenditures and hope for the future.

+3
Apr 6 '10 at 22:00
source share

The short version is that classical classes that did not need a superclass had limitations that could not be circumvented without parsing a lot of old code. Thus, they created the concept of new-style classes that are a subclass of object , and now you can do cool things, such as defining properties, and subclassing dict no longer an exercise in pain and strange mistakes.

Details are provided in section 3.3 of the Python docs: New and classic classes.

0
Apr 6 2018-10-06T00:
source share

Python 2.2 introduced the "new style classes" , which had a number of additional features compared to the old style classes, which are not a subclass of the object. The Subclasses object was the chosen way to indicate that your class should be a new style class, not an old one.

0
Apr 6 2018-10-06T00:
source share



All Articles