Python 继承标准类时发生了什么
2013-11-17 22:19
225 查看
定义标准类dict的一个子类c:
可见,子类反而比父类多了3个属性:
{'__module__', '__weakref__', '__dict__'}
暂时不知有何深意.
>>> 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__'}
暂时不知有何深意.
相关文章推荐
- 关于为什么在有继承关系的类结构中,析构函数destructor要是virtual以及如果不是会发生什么
- Python - 子类继承父类 和 Java有什么区别
- static静态方法可以被继承吗?Student a= new Student(); new一个对象的过程发生了什么?
- Python实例一个类背后发生了什么
- 继承关系下new子类对象,会发生什么?
- Python实例一个类背后发生了什么
- (转载)Android从按下开机键到启动发生了什么
- C++ exception throw和catch的时候,发生了什么
- [python]闭包到底是什么鬼?
- Python中的类型关系和继承关系
- Python tips: 什么是*args和**kwargs
- VisionMobile:非对称商务模式(七)如果是非对称模式竞争的对手会发生什么?
- 远程和ping都连不上服务器,发生什么了?
- 在浏览器中输入网址按下回车后都发生了什么(整体概况)
- Python+Selenium框架设计篇之3-什么是POM
- Python中字符串的split()方法和split(‘ ’)有什么区别
- Python 学习笔记 - 面向对象(封装,继承和多态)
- 当你访问淘宝的时候,发生了什么?
- Python到底能干什么
- python类继承需要注意的问题