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

6

67

All the Python built-ins are subclasses of object and I come across many user-defined classes which are too. Why? What is the purpose of the class object? It's just an empty class, right?

Ganister answered 6/4, 2010 at 21:54 Comment(0)
H
63

Note: new-style classes are the default in Python 3. Subclassing object is unnecessary there. Read below for more information on usage with Python 2.

In short, it sets free magical ponies.

In long, Python 2.2 and earlier used "old style classes". They were a particular implementation of classes, and they had a few limitations (for example, you couldn't subclass builtin types). The fix for this was to create a new style of class. But, doing this would involve some backwards-incompatible changes. So, to make sure that code which is written for old style classes will still work, 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 and class Foo(object): pass will create a new style class.

In longer, see Guido's Unifying types and classes in Python 2.2.

And, in general, it's a good idea to get into the habit of making all your classes new-style, because some things (the @property decorator is one that comes to mind) won't work with old-style classes.

Horsewhip answered 6/4, 2010 at 22:1 Comment(1)
@thetaiko lists some other new-style-only things that are good to know about.Horsewhip
E
13

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

For the difference between new style classes and old style classes: see this stackoverflow question. For the complete story: see this nice writeup on Python Types and Objects.

Endophyte answered 6/4, 2010 at 22:4 Comment(0)
B
4

It has to do with the "new-style" of classes. You can read more about it here: http://docs.python.org/tutorial/classes.html#multiple-inheritance and also here: http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes

Using new-style classes will allow you to use "Python's newer, versatile features like __slots__, descriptors, properties, and __getattribute__()."

Beata answered 6/4, 2010 at 22:2 Comment(0)
W
3

Right, but it marks the class as a new-style class. Newly developed classes should use the object base because it costs little and future-proofs your code.

Woodhouse answered 6/4, 2010 at 22:0 Comment(0)
Q
1

The short version is that classic classes, which didn't need a superclass, had limitations that couldn't be worked around without breaking a lot of old code. So they created the concept of new-style classes which subclass from object, and now you can do cool things like define properties, and subclassing dict is no longer an exercise in pain and strange bugs.

The details are in section 3.3 of the Python docs: New-style and classic classes.

Questor answered 6/4, 2010 at 22:3 Comment(0)
I
0

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

Isocracy answered 6/4, 2010 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.