Python 继承标准类时发生了什么
生活随笔
收集整理的這篇文章主要介紹了
Python 继承标准类时发生了什么
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定義標準類dict的一個子類c:
>>> class c(dict):pass>>> y=c({1:2,3:4}) >>> y {1: 2, 3: 4} >>> y.__dict__ {} >>> z={1:2,3:4} >>> z.__dict__ Traceback (most recent call last):File "<pyshell#98>", line 1, in <module>z.__dict__ AttributeError: 'dict' object has no attribute '__dict__' >>> dir(y) ['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] >>> dir({}) ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] >>> set(dir(y))-set(dir({})) {'__module__', '__weakref__', '__dict__'}可見,子類反而比父類多了3個屬性:
{'__module__', '__weakref__', '__dict__'}
暫時不知有何深意.
轉載于:https://www.cnblogs.com/xiangnan/p/3428445.html
總結
以上是生活随笔為你收集整理的Python 继承标准类时发生了什么的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bitmap的管理
- 下一篇: 电子书推荐--《Python灰帽子》,p