0

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

4

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):
    ...
Community
  • 1
  • 1
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
3

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.

Community
  • 1
  • 1
m00am
  • 5,910
  • 11
  • 53
  • 69