Difference between class declarations
There is no difference between the first two spellings.
In python 2.7, there is a huge difference between the latter two. Inheritance of object
making it a new-style class , changing the semantics of inheritance and adding support for tags ( @property
, @classmethod
, etc.). This is the default in Python 3.
Python 2.2 introduced new style classes to unify types (eg, int
and list
) and classes, and because several things change in the opposite direction are incompatible, you need to "select" explicitly inherit from object
to enable changes.
In Python 3, inheritance from is object
no longer required, classes are always new.
source to share
There is no difference between class MyClass
and class MyClass()
. The second question depends on your python version. There is no difference on python3.x - on python2.x the latter (where you inherit from object
) creates a new style class , not an old style. In python3.x, ALL classes are new. New style classes are preferred these days. This way, I always make sure my classes inherit from the object.
source to share
Type Class Declaration class MyClass(object)
New Style Classes in Python 2.x
Guido writes about some of the thoughts that sparked new classes in Python History
source to share