python的class(类)中的object是什么意思?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python的class(类)中的object是什么意思?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2382字,纯文字阅读大概需要4分钟。
内容图文
![python的class(类)中的object是什么意思?](/upload/InfoBanner/zyjiaocheng/755/1cb96e48953549a9a4c8ba7974d55a58.jpg)
那写object和不写object有什么区别?
好的,再用代码来理解它们的区别.
#?-.-?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)
运行结果:
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__ |
---------------------
原文:https://blog.csdn.net/DeepOscar/article/details/80947155
内容总结
以上是互联网集市为您收集整理的python的class(类)中的object是什么意思?全部内容,希望文章能够帮你解决python的class(类)中的object是什么意思?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。