4

What is the difference between the two? Don't both of these have the same functionality? I don't understand the whole point of the object parameter.

class Car(object): # object parameter
    def foobar():
        print("Hello World!\n")  

vs.

class Car(): # No parameter
    def foobar():
        print("Hello World!\n")
Bach
  • 6,145
  • 7
  • 36
  • 61
user1757703
  • 2,925
  • 6
  • 41
  • 62
  • 1
    The first is a new style class, and the latter is an old style class. See http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python?rq=1 – dustyrockpyle Jun 27 '14 at 07:12

2 Answers2

3

In Python 2, the former is a "new-style class" and the latter is an "old-style class" that only exists for backwards compatibility. You should never use the latter for anything new.

In Python 3, I believe there is no difference at all. You can even leave out the parentheses entirely.

Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
0

In two words:

# new style
class Car(object):
    pass

A "New Class" is the recommended way to create a class in modern Python.

# classic style
class Car():
    pass

A "Classic Class" or "old-style class" is a class as it existed in Python 2.1 and before. They have been retained for backwards compatibility. This page attempts to list the differences.

Please look at:

Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52