3

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?

0

2 Answers 2

6

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):
    ...
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.