4

I'm learning python and introducing myself to OOP. However, I'm struggling to understand how best to build classes and particularly, what the difference between the following class definitions are and when I should use each:

class my_class:
  content...

class my_class():
  content...

class my_class(object):
  content...

I have been reading the very useful python online help, although not found a specific answer to this question. So any ideas or recommended references would be appreciated, thanks.

IanRoberts
  • 2,846
  • 5
  • 26
  • 33

2 Answers2

5

Well, I can immediately say that there is nothing special about the second method:

class my_class():
  content...

In the above code, the parenthesis are redundant and do not affect the class definition at all. The reason why Python allows you to have them is explained by @chepner's comment. Basically, because the list of base classes (the code placed inside the parenthesis) may be any valid expression, () is allowed because it is syntactically valid.


The purpose of the other two methods depends on what version of Python you are using.

Python 2.x

The following code will create an old-style or classic class:

class my_class:
  content...

In Python versions before 2.2, old-style classes were the only type of classes available. According to Python's documentation on them:

The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x, but type(x) is always <type 'instance'>. This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance.

However, in Python versions 2.2 or greater, this all changes if you inherit from the object built-in:

class my_class(object):
  content...

Now, you will be creating a new-style class, which has an entirely different object model. Unlike its predecessor, a new-style class:

...is neither more nor less than a user-defined type. If x is an instance of a new-style class, then type(x) is typically the same as x.__class__...

If you would like some more information on what the differences between old-style and new-style classes are, see this great SO question which asks exactly that.

Python 3.x

In Python 3.x, all classes implicitly inherit from object and are thus new-style classes.

This means that it is now redundant to do:

class my_class(object):
  content...

because doing this:

class my_class:
  content...

will yield the same results. However, many programmers still explicitly inherit from object to make their classes backwards-compatible with older versions of Python.

Community
  • 1
  • 1
  • According to the [grammar](http://docs.python.org/2/reference/grammar.html), the list of base classes is allowed to be a full blown expression (using `and`, `or`, `if ... else`, lambdas), not just a simple tuple of names. A side effect of that is that the list can be empty. – chepner Mar 27 '14 at 15:25
  • @chepner - Thanks for the info. I'll add it to my answer. –  Mar 27 '14 at 15:35
  • the new-style classes have better performance as well, so if you don't really care I make it a habit of always inheriting from object. – capturesteve Mar 27 '14 at 15:44
  • @capturesteve - I agree. In Python 2.x, I also always inherit from `object` because of the superiority of new-style classes over old-style ones. In Python 3.x however, doing so seems redundant to me, unless you plan to have your class used in older versions. –  Mar 27 '14 at 15:51
  • Thanks, I'm using 2.7 so for now will get used to always inheriting from object as that seems a better approach. – IanRoberts Mar 29 '14 at 09:26
1

Behaviour in Python 2.2+:

class definitions A and B behave the same way, however, C inherits from object:

class A:
    pass

class B():
    pass

class C(object):
    pass


>>> A.__dict__
{'__module__': '__main__', '__doc__': None}

>>> B.__dict__
{'__module__': '__main__', '__doc__': None}

>>> C.__dict__
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

There are many more differences in behavior of the two definitions. See Guido van Rossum's post on the matter, as well as the python documentation below.

References

New style and classic style classes:

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

http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes

pygeek
  • 7,356
  • 1
  • 20
  • 41