0 votes
1 view
in Python by (31.4k points)

What is the difference between old style and new style classes in Python? When should I use one or the other?

1 Answer

0 votes
by (73.5k points)

Following are the ways how we declare old and new-style class:

One new-style class inherit from an object or from another new-style class.

class NewStyleClass(object): 

Pass

class AnotherNewStyleClass(NewStyleClass): 

pass

The old-style class does not inherit from anywhere:-.

class OldStyleClass():

pass

If we see compatibility reasons, classes are still old-style by default.

To create new-style classes we need to specify another new-style class (i.e. a type) as a parent class, or the "top-level type" object if no other parent is needed.

The behavior of old-style classes differs from that of new-style classes in a number of important details in addition to what type returns.

In Python 3 we only have new-style classes.

...