Danielle B. answered 9h
Computer Science Tutor Specializing in Python and Data Science
Hi there,
New style classes were introduced in Python 2.2 and exist in both Python 2 and Python 3.
Python 2:
Old style classes: do not inherit from object
for example:
class OldStyle:
pass
New style classes: explicitly inherit from object
for example:
class NewStyle(object):
pass
Python 3:
Old style classes no long exist and every class is automatically new style.
If you are working in Python 3, there is no real difference and you'll never need to choose.
If you are working in Python 2, the noticeable differences are:
- Method Resolution Order - new style classes determine which parent's method to call in multiple inheritance situations.
- type() behavior - old style classes returned unhelpful information
- @property, @staticmethod and @classmethod only work correctly with new style classes
So, when should you use one or the other:
- Python 3 - uses new style automatically
- Python 2 - always explicitly inherit from object to get new style behavior