Python中的類在聲明的時候繼承object的原因

首先說明python3中無論寫不寫(object),默認的會自動帶上,所以在python3中寫不寫都一樣
class Student1:
    pass

class Student2(object):
    pass
print(dir(Student1()))
print(dir(Student2()))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
可以看出兩個的所具有相同的屬性,因爲繼承了object
若不繼承則只有__doc__ , __module__ 這兩個屬性,因爲上邊的屬性,我們都要使用,所以一般是繼承的。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章