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")
1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So is there a technically right way to do it?
@user1757703 As you probably want new-style classes, always use (object) if you want to stay backward-compatible.
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:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.