Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for Python 3 class creation

In my research I found that in Python 3 these three types of class definition are synonymous:

class MyClass:
    pass

class MyClass():
    pass

class MyClass(object):
    pass

However, I was not able to find out which way is recommended. Which one should I use as a best practice?

like image 899
Tim Keller Avatar asked Aug 23 '17 07:08

Tim Keller


1 Answers

I would say: Use the third option:

class MyClass(object):
    pass

It explicitly mentions that you want to subclass object (and doesn't the Zen of Python mention: "Explicit is better than implicit.") and you don't run into nasty errors in case you (or someone else) ever run the code in Python 2 where these statements are different.

like image 147
MSeifert Avatar answered Sep 28 '22 10:09

MSeifert