I am trying to learn Python 3. In a book I read, to define a class you use:
class Classname:
# and so on
In another it says:
class Classname(object):
# and so on
Which is the right way to do it? How are they different?
I am trying to learn Python 3. In a book I read, to define a class you use:
class Classname:
# and so on
In another it says:
class Classname(object):
# and so on
Which is the right way to do it? How are they different?
there is a difference only for python 2.7: old-style and new-style classes in Python 2.7?
in python 3
class SomeClass:
...
is the same as
class SomeClass(object):
...
In the specific case that you mention (i.e. inheriting from object
) this makes no difference in Python 3. In Python 2 this was a distinction between old style classes
class Classname:
...
and new style classes
class Classname(object):
...
which behaved differently as described here and here.
As you are programming in Python 3 I would just omit it to make your code easier to read. Since all objects implicitly inherit from object
this information is not helpful to a reader of your code.