2

What is the difference between saying:

class foo:
    pass

and

class foo(object):
    pass

?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
reza
  • 21
  • 1

3 Answers3

4

The latter declares a new-style class.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • What a great community. Thanks everybody and pardon about asking duplicate question ;) – reza Dec 16 '10 at 13:36
  • 2
    Keep in mind that it is only different in Python 2.x. In Python 3.x, both class definitions create "new-style" classes (the only type of class available). – brildum Dec 16 '10 at 13:49
  • @reza: If the answer is what you were looking for, please accept it using the tick on the left. – Björn Pollex Dec 16 '10 at 14:00
3

"Classes and instances come in two flavors: old-style (or classic) and new-style." http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes

Paweł Prażak
  • 3,091
  • 1
  • 27
  • 42
2

Old-style classes don't extend the built-in 'object' type. New-style classes do. If you're writing new code, use new-style classes. If you're working with old code that is already using old-style classes, well... I say go with it just to insure that all classes act the same way.

Here's a good rundown of the differences: http://docs.python.org/release/2.5.2/ref/node33.html And here's a collection of links for more: http://www.python.org/doc/newstyle/

I'll note that new-style classes were introduced in Python 2.2, so if you're seeing code that uses old-style classes, and you're going to use it with, say, Python 2.7, you might want to run a quick test to make sure it's not so aged that it doesn't work in newer versions of Python.

jonesy
  • 3,502
  • 17
  • 23