python的class(类)中的object是什么意思?
2016-03-13 00:00
645 查看
摘要: 为什么在写class的时候要加上object,不加又怎样?
上篇博文提到LEGB原则,其中最后一个B原则就是内置(built-in)命名空间,
它会将python内部的__builtin__.py实例化的内容进行检索. 什么意思呢? 这里先列出一段简短的代码。
通过这段代码,当我们实例化Person()这个类的时候,那x就是一个实例对象, 整个过程python除了创建Person这个类的命名空间之外(把name=”zhengtong”加入到命名空间中),还会去执行__builtin__.py中的object类,并将object类中的所有方法传承给Person(也就是说Person继承了object的所有方法).
回到主题上来,那写object和不写object有什么区别?
好的,再用代码来理解它们的区别.
运行结果
Person类很明显能够看出区别,不继承object对象,只拥有了__doc__ , __module__ 和 自己定义的name变量, 也就是说这个类的命名空间只有三个对象可以操作.
Animal类继承了object对象,拥有了好多可操作对象,这些都是类中的高级特性。
对于不太了解python类的同学来说,这些高级特性基本上没用处,但是对于那些要着手写框架或者写大型项目的高手来说,这些特性就比较有用了,比如说tornado里面的异常捕获时就有用到__class__来定位类的名称,还有高度灵活传参数的时候用到__dict__来完成.
最后需要说清楚的一点, 本文是基于python 2.7.10版本,实际上在python 3 中已经默认就帮你加载了object了(即便你没有写上object)。
这里附上一个表格用于区分python 2.x 和 python 3.x 中编写一个class的时候带上object和不带上object的区别.
上篇博文提到LEGB原则,其中最后一个B原则就是内置(built-in)命名空间,
它会将python内部的__builtin__.py实例化的内容进行检索. 什么意思呢? 这里先列出一段简短的代码。
[code=plain]# -.- coding:utf-8 -.- # __author__ = 'zhengtong' class Person(object): name = "zhengtong" if __name__ == "__main__": x = Person()
通过这段代码,当我们实例化Person()这个类的时候,那x就是一个实例对象, 整个过程python除了创建Person这个类的命名空间之外(把name=”zhengtong”加入到命名空间中),还会去执行__builtin__.py中的object类,并将object类中的所有方法传承给Person(也就是说Person继承了object的所有方法).
回到主题上来,那写object和不写object有什么区别?
好的,再用代码来理解它们的区别.
[code=plain]# -.- coding:utf-8 -.- # __author__ = 'zhengtong' class Person: """ 不带object """ name = "zhengtong" class Animal(object): """ 带有object """ name = "chonghong" if __name__ == "__main__": x = Person() print "Person", dir(x) y = Animal() print "Animal", dir(y)
运行结果
[code=plain]Person ['__doc__', '__module__', 'name'] Animal ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
Person类很明显能够看出区别,不继承object对象,只拥有了__doc__ , __module__ 和 自己定义的name变量, 也就是说这个类的命名空间只有三个对象可以操作.
Animal类继承了object对象,拥有了好多可操作对象,这些都是类中的高级特性。
对于不太了解python类的同学来说,这些高级特性基本上没用处,但是对于那些要着手写框架或者写大型项目的高手来说,这些特性就比较有用了,比如说tornado里面的异常捕获时就有用到__class__来定位类的名称,还有高度灵活传参数的时候用到__dict__来完成.
最后需要说清楚的一点, 本文是基于python 2.7.10版本,实际上在python 3 中已经默认就帮你加载了object了(即便你没有写上object)。
这里附上一个表格用于区分python 2.x 和 python 3.x 中编写一个class的时候带上object和不带上object的区别.
python 2.x | python 2.x | python 3.x | python 3.x |
不含object | 含object | 不含object | 含object |
__doc__ | __doc__ | __doc__ | __doc__ |
__module__ | __module__ | __module__ | __module__ |
say_hello | say_hello | say_hello | say_hello |
__class__ | __class__ | __class__ | |
__delattr__ | __delattr__ | __delattr__ | |
__dict__ | __dict__ | __dict__ | |
__format__ | __format__ | __format__ | |
__getattribute__ | __getattribute__ | __getattribute__ | |
__hash__ | __hash__ | __hash__ | |
__init__ | __init__ | __init__ | |
__new__ | __new__ | __new__ | |
__reduce__ | __reduce__ | __reduce__ | |
__reduce_ex__ | __reduce_ex__ | __reduce_ex__ | |
__repr__ | __repr__ | __repr__ | |
__setattr__ | __setattr__ | __setattr__ | |
__sizeof__ | __sizeof__ | __sizeof__ | |
__str__ | __str__ | __str__ | |
__subclasshook__ | __subclasshook__ | __subclasshook__ | |
__weakref__ | __weakref__ | __weakref__ | |
__dir__ | __dir__ | ||
__eq__ | __eq__ | ||
__ge__ | __ge__ | ||
__gt__ | __gt__ | ||
__le__ | __le__ | ||
__lt__ | __lt__ | ||
__ne__ | __ne__ |
相关文章推荐
- Json反序列化之ObjectMapper(自定义实现反序列化方法)
- 第一章-熟悉Objective-C
- Object-c对象和消息传递
- Objective-c 深浅复制
- 从C/C++到Objective-C(五)--- 类别的使用
- android.os.DeadObjectException memory near r0: 异常处理
- js中object、字符串与正则表达式的方法
- Objective-c 集合对象
- 分离出表单对象(Form Objects)
- MyBatis中对List<Object> 对象List的批处理插入操作
- 从C/C++到Objective-C(四)--- 属性的使用
- Object-C HelloWorld 编译运行
- Objective-c 字典对象
- Android之–Activity通过Intent传递Object
- Object-C与Swift混合开发
- git :设置 object-c 的忽略文件
- Example rebuttal for "YOLO: Real-Time Object Detection"
- objective-c HelloWorld实践 (gcc编译器)
- Java Object Layout(jol)
- AttributeError: 'NoneType' object has no attribute 'bytes' python3.4 win64