views:

70

answers:

3

Is there any reason for a class declaration to inherit object?

I just found some code that does this and I can't find a good reason why.

class MyClass(object):
    # class code follows...

The code is using swig for binding some C code to Python, if that's relevant.

+1  A: 

This creates a new-style class.

SLaks
+1  A: 

Yes, it's historical. Without its old-style classes.

If you use type() on an old-style object, you just get "instance". On a new-style object you get its class

knitti
+7  A: 

Yes, this is a 'new style' object. It was a feature introduced in python2.2.

New style objects have a different object model to classic objects, and some things won't work properly with old style objects, for instance, super(), @property and descriptors. See this article for a good description of what a new style class is:

http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html

SO link for a description of the differences: http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python

Jerub
+1 This. Note that old-style classes are gone in Python 3, so you only need to inherit from `object` in Python 2.
delnan