Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k 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 (106k 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): 


class AnotherNewStyleClass(NewStyleClass): 


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

class OldStyleClass():


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.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories