obj = object(),所创建的obj实例到底是个啥?
In[1]: dir(obj)
Out[1]:['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
?
和我们对任何常规的对象调用dir函数一样,得到的是一堆带双前后导下划线的特殊属性,现在来做点有意义的事情:对他们进行分类
1.rich comparison operator:
__lt__, __gt__, __le__, __ge__, __eq__, __ne__
2.attribute management
__getattribute__, __delattr__, __setattr__, __dir__
特别要注意的是,这里没有__getattr__,因为它只有在试图访问一个不存在的属性时,由__getattribute__方法报AttributeError错误后才会调用
3.string representation
__repr__, __str__, __format__
4.instance creation and destruction
__new__, __init__, __init_subclass__
5.memory management
__sizeof__(一般会调用sys.getsizeof())
6.comversion to number
__hash__
7.pickling
__reduce__, __reduce_ex__
与对象的序列化相关的特殊方法。这里的reduce是用于控制自定义类的扩展类行为,不是fuctools中的reduc()函数所触发的方法。
8.virtual subclass implicit registering
__subclasshook__
实现这个特殊方法后,解释器会自动检测某个自定义的类是否定义了一些指定的特殊方法,检测的实质是在自定义的类(及其各级父类)的属性__dict__中寻找是否出现特殊方法。
上述1-8皆为特殊方法,下面的9显示了这个最普通的object类的实例obj的特殊属性及其值:
9.
1)__class__:object
2)__doc__:'The most base type'
最新文章
- 【腾讯Bugly干货分享】WebVR如此近-three.js的WebVR示例解析
- Man——send(2)翻译
- JS获取当前时间并把时间赋给input表单
- visual studio 引用lib
- Jade之注释
- Listview之优化BaseAdapter中的getView中的contentView
- css 利用border属性制作箭头 Using Borders to Make Pure CSS Arrows
- 我的django之旅(一)
- SDK Manager 报错:Connection timed out: connect
- js面向对象oop编程
- 仿中关村win8频道(win8.zol.com.cn)下的tab效果
- 模仿ICE的structured panorama小按钮
- 【codevs1001】[bzoj1050]舒适的路线
- TurnipBit口袋编程计算机:和孩子一起DIY许愿的流星
- pandas教程
- 基于SpringBoot+Mybatis+AntDesign快速开发平台,Jeecg-Boot 1.1 版本发布
- html跳动的心实现代码
- 100.Same Tree(E)
- VUE CLI 3.0 项目引入 ElementUI
- Codeforces-gym-101020 problem C. Rectangles
热门文章
- centos6 编译安装gcc4.8.2
- Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)
- 分类算法简介 基于R
- kmeans聚类中的坑 基于R shiny 可交互的展示
- Thinkphp 5 使用DOMDocument
- 使用jdk进行数据迁移(sqlite迁移mysql)
- Git基础篇
- django视图层(views)
- WebStrome react-native代码智能提示
- Map the Debris -freecodecamp算法题目