0

In python I can create class as follows:

class Car:

    def __init__(self):
       pass

But I have seen some people create class with empty brackets e.g.:

class Car():

    def __init__(self):
       pass

The second form works, so my question is which one is the correct (or pythonic) way of defining class. I know that in brackets I can provide another class for inheritance e.g. class Car(Vehicle):, so when a class is created with empty brackets does it explicitly imply no inheritance?

2
  • 1
    Both are identical Commented Nov 29, 2022 at 6:24
  • The brackets are used for inheritance. If you don't want your class to inherit any class you can just leave them out Commented Nov 29, 2022 at 9:37

1 Answer 1

1

In python3, you can just ignore the brackets.

In other words, class Car is equivalent to class Car() and the former one is more pythonic. However, In python2, you have to use class Car(object) to suit python2's requirement.

Sign up to request clarification or add additional context in comments.

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.