Understanding Python Class Inheritance
In Python, classes can inherit from other classes, which grants them access to the parents' attributes and methods. However, one peculiarity that often arises is why classes are declared to inherit from the object class.
Why Inherit from object (Python 2.x vs. Python 3.x)
In Python 2.x, class declarations without an explicit parent class are known as "classic" classes. These classes do not inherit from object and have a number of limitations compared to "new" style classes, which explicitly inherit from object:
In Python 3.x, the distinction between classic and new style classes is eliminated. All classes are considered new style classes and implicitly inherit from object without the need for explicit declaration.
Should You Inherit from object in Python 3?
While inheriting from object is optional in Python 3, it does not provide any benefits or drawbacks. However, for compatibility reasons, it is recommended to inherit from object when writing code that may need to run in both Python 2.x and Python 3.x.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3