0

I've had a little confusion regarding old and new style classes in python. So basically in Python 3.X there are just new style classes that can both inherit and not inherit from other classes, while in Python 2.X new style classes must be subclasses of another class or an object, while old-style classes can't inherit at all. Is that right?

PMM
  • 366
  • 1
  • 10
  • 2
    No, if you inherit from an old-style class you get an old-style class. – Ry- Jun 14 '19 at 20:01
  • 1
    Possible duplicate of [What is the difference between old style and new style classes in Python?](https://stackoverflow.com/questions/54867/what-is-the-difference-between-old-style-and-new-style-classes-in-python) –  Jun 14 '19 at 20:03

1 Answers1

2

No. In Python 2, to make a class a new-style class, it had to inherit from object (or from some superclass that did). Old-style classes would inherit other old-style classes, but the base ones didn't inherit from object; they were just written class MyClass:.

Mostly, when new classes came out people started writing class MyClass(object): and for 99% of uses everything stayed the same. The differences were in double-underscore methods and other low level details that didn't matter for most code.

The reasoning was to remove the difference between built-in types and user-defined classes; Guido's reasoning can be read here.

And now that is history, Python 3 just has classes. Python 2.2 introduced new-style classes and it was released December 21, 2001. You don't need to know this.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • Thank you very much, you were really helpful! – PMM Jun 14 '19 at 20:11
  • If you feel that this answer helped you @PietroMarsella, you should consider [what to do when someone answers my question](https://stackoverflow.com/help/someone-answers), and [how to accept my answer](http://meta.stackexchange.com/a/5235) – Reblochon Masque Jun 16 '19 at 12:38