Python-100-Days
Python-100-Days copied to clipboard
Day08 class Student(object): object 存在的意义?()存在的意义?;全部删除也正常运行;为何要多此一举?
Day08 class name(object): object 存在的意义?()存在的意义?;全部删除也正常运行;为何要多此一举?
python2/3区别 经典类 新式类
对于Python3来说确实是没有区别的,后续代码我都会处理掉。
The presence of (object) in the class name(object): declaration has significance in Python. In Python, when defining a class, it is common to inherit from the object class. Although in Python 3, this inheritance declaration can be omitted, it is still recommended to explicitly inherit from the object class for the sake of code compatibility and readability.
The object class is the base class for all classes in Python and provides some built-in methods and attributes such as init(), str(), and repr(). By inheriting from the object class, we can utilize these built-in methods and attributes, giving our custom class more functionality and features.
Although removing (object) from class name(object): will not affect the code execution, it is advisable to keep this inheritance declaration for consistency and clarity in both Python 2 and Python 3. This helps avoid potential compatibility issues between different versions of Python and makes the code easier to understand and maintain.