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?
All the Python built-ins are subclasses of object and I come across many user-defined
Share
Note: new-style classes are the default in Python 3. Subclassing
objectis 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
objectclass was created to act as a superclass for all new-style classes.So, in Python 2.X,
class Foo: passwill create an old-style class andclass Foo(object): passwill 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
@propertydecorator is one that comes to mind) won’t work with old-style classes.