4

When creating a class in python should it inherit from object or Object, or neither? Is there any need to inherit from object at all?

class NewClass(object)

or

class NewClass(Object)

or

class NewClass()
Scone
  • 669
  • 6
  • 11
  • 6
    Inheriting from `object` creates a ["new-style" class](http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python). You should always use this for new code in general. `Object` (with a capital O) doesn't exist in Python. And not inheriting from anything gives you an "old-style" class. – Cameron Jan 26 '13 at 04:38
  • Very concise, but informative answer. Thanks. – Scone Jan 26 '13 at 04:45

1 Answers1

5

A class inherits from object if it 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:

Python Documentation - Type and Class Changes

Object, on the other hand, seems to be a poorly named variable or object.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • 1
    Very clear thanks. I guess I was confused because my IDE was highlighting Object (capital O) and it looked like a keyword to me. – Scone Jan 26 '13 at 04:45