Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class definition parameter vs. Normal class definition [duplicate]

Tags:

python

class

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")
like image 301
user1757703 Avatar asked Oct 15 '25 15:10

user1757703


1 Answers

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.

like image 71
Andrew Gorcester Avatar answered Oct 17 '25 04:10

Andrew Gorcester