31

What is the difference between:

class ClassName(object):
    pass

and

class ClassName:
    pass

When I call the help function of the module of those class you can read ____builtin____.object for the first case just under the CLASS title of help. For the second case it just shows the class name. Is there any functional difference between those classes and/or possible methods thereof?

(I know that class Classname(ParentClassName) has a functional use)

Bentley4
  • 10,678
  • 25
  • 83
  • 134
  • 2
    The first is a newstyle class, the other is an oldstyle class. You may want to look at [this SO answer](http://stackoverflow.com/a/54873/1079354) for a decent difference between the two. – Makoto Apr 06 '12 at 13:14
  • Just to add to it, although you don't need to do `class ClassName(object):` in python3 I think you should still do it. Its clearer – Jakob Bowyer Apr 06 '12 at 13:22
  • I checked it. I don't understand all the differences pointed out in other threads. But in those threads the majority seems to advise using the new style. Strangely the classes of the modules of the standard library of python2.7 only use the old style. (check the class `Timer` of the module `timeit` e.g.) – Bentley4 Apr 06 '12 at 13:40

2 Answers2

25

In Python 2.x, when you inherit from "object" you class is a "new style" class - that was implemented back in Python 2.2 (around 2001) - The non inheriting from "object" case creates an "old style" class, that was actually maintained only for backwards compatibility.

The great benefit of "new style" classes is the unification of types across Python - prior to that, one could not subclass built-in types such as int, list, dict, properly. There was also specified a "descriptor protocol" which describes a protocol for retrieving and setting attributes in an object, giving the language a lot of flexibility. (It is more visible when one does use a Python "property" in a class).

What does make the difference is not actually "inheriting from object", but, since classes in Python are also objects, that does change the class'class itself (a class'class is known as its "metaclass"). Thus if you set the metaclass to be "type", you don't need to inherit from object to have a new style class.

It is strongly recommended that in Python 2.x, all your classes are new style - using old style classes may work for some single straightforward cases, but they can generate a lot of subtle, difficult to find, errors, when you try to use properties, pickle, descriptors, and other advanced features. Above all, when you try to check the "type" of an object, it will be the same (type "instance") for all objects from old style classes, even if they are from different user defined classes.

In Python versions 3.x all classes are new style - no need to set the metaclass.

Python's documentation "datamodel" is the "book of law" where the behavior of both class typs is defined in detail (enough to allow one to reimplement the language):

http://docs.python.org/reference/datamodel.html

This blog post from Guido talks about the motivations behind new style classes in a lighter language:

http://python-history.blogspot.com.br/2010/06/new-style-classes.html

http://docs.python.org/release/2.5.2/ref/node33.html

jsbueno
  • 99,910
  • 10
  • 151
  • 209
8

ClassName(object) uses the new style class: http://docs.python.org/release/2.5.2/ref/node33.html

The second example demonstrates an old style class.

In python 3, new style classes are used by default and you will no longer need to subclass object.

jamylak
  • 128,818
  • 30
  • 231
  • 230