Python-100-Days icon indicating copy to clipboard operation
Python-100-Days copied to clipboard

Day08 class Student(object): object 存在的意义?()存在的意义?;全部删除也正常运行;为何要多此一举?

Open lionderful opened this issue 3 years ago • 3 comments

Day08 class name(object): object 存在的意义?()存在的意义?;全部删除也正常运行;为何要多此一举?

lionderful avatar Aug 29 '21 12:08 lionderful

python2/3区别 经典类 新式类

zhangyue66 avatar Aug 29 '21 21:08 zhangyue66

对于Python3来说确实是没有区别的,后续代码我都会处理掉。

jackfrued avatar Nov 20 '21 12:11 jackfrued

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.

ggggggggggggg1 avatar Nov 26 '23 12:11 ggggggggggggg1