0

What is the exact difference among these three and when do we use what?

class A:
    pass
class A(object):
    pass

or

class A():
    pass
  • There is no functional difference. – MisterMiyagi Jan 21 '21 at 07:34
  • @MisterMiyagi Not in Python 3, but in Python2. https://stackoverflow.com/questions/54867/what-is-the-difference-between-old-style-and-new-style-classes-in-python – Klaus D. Jan 21 '21 at 07:40
  • @KlausD. Since Python 2 is EOL, I do not consider it relevant to questions unless specifically asked for or tagged. Leave it to the poor souls like me who have some legacy apps to maintain. – MisterMiyagi Jan 21 '21 at 08:44

1 Answers1

1
class A:
    pass

It is implicitly subclass of object (as in other cases). I think it's most prefereable in case you don't inherit from anything (but it can depend on coding standards).

2)

class A(object):
    pass

It is most explicit version.

3)

class A():
    pass

In this case, as no class passed as parent class, by default it inherits from object.

So from functionality point of view, there is no difference. In Python3, all clasess inherit from object (even if it's not explixitly declared). However, if you are using Python2, you need to pass superclass explicitly in every case.

Dawid Gacek
  • 544
  • 3
  • 19