python的class(类)中的object是什么意思?
原文地址:https://blog.csdn.net/DeepOscar/article/details/80947155
那写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__ |
python的class(类)中的object是什么意思?相关推荐
- python定义类()中写object和不写的区别
python定义类()中写object和不写的区别 这里需要说明一下: python3中,类定义默认继承object,所以写不写没有区别 但在python2中,并不是这样 所以此内容是针对python ...
- Python(私有变量)类中的特殊方法
2019独角兽企业重金招聘Python工程师标准>>> 类中的特殊方法 一般说来,特殊的方法都被用来模仿某个行为.例如,如果你想要为你的类使用x[key]这样的索引操作(就像列表和元 ...
- Python 类中的object是什么
在python中,object是一个我们会经常遇到的词汇,那么这个词汇究竟有什么含义呢?通过查阅一些文档,我们发现,object其实是一个类的实例,而这个类的名字是Object(默认类的命名首字母大写 ...
- python语法:类中object的用法
假如定义两个类,一个带object,一个不带object,会不会有差别呢? class Cat:name = "mao" class Dog(object):name = &quo ...
- python多线程执行类中的静态方法
在python 中如果通过多线程的方式执行某个方法很简单,只需要把同步函数的第一个参数为该函数对象即可.但是如果函数对象是某个类的静态方法,这时候如果直接使用类的该函数对象会报错.此时需要构造一个代理 ...
- python iter next_python类中的__iter__, __next__与built-in的iter()函数举例
i1 = iter(itr, 'c') 这个意思是说,返回itr的iterator,而且在之后的迭代之中,迭代出来'c'就立马停止.对这个itr有什么要求呢?这个itr在这里必须是callable的, ...
- python调用其他类中的方法_无法调用其他类中的方法
以下是页面对象文件:登录名.py在from pages.base import BasePage from config import secrets from selenium.webdriver. ...
- python中的object是什么意思_Python object类中的特殊方法代码讲解
python版本:3.8class object: """ The most base type """ # del obj.xxx或del ...
- python里object是什么类型_Python中的object这个叫做基类的类,它的属性和行为都有哪些?...
声明:以下仅为个人学习Python过程中总结的比较自洽的理解,事实上Python面向对象模型可能没那么简单.经查阅,Python在以前的版本还有不继承自object的类,叫做经典类. 用dir(obj ...
最新文章
- mysql对时间操作系统_MySQL时间操作的系统函数用法
- System.Windows.Forms.Timer在线程中的注意事项
- 2.1.5 线程概念和多线程模型
- 15.profile-api
- 人工智能诗歌写作平台_智能写作VS人工写作,Giiso写作机器人解放你的创造力...
- SubVersion服务器Windows安装过程
- iOS扩大按钮的点击范围
- DMN结合bpmn简化流程_07
- chromium浏览器_轻松下载便携式Chromium浏览器
- 一种绘制有向图的方法<TSE93> - 1. 引言
- 【国内动态】服务器列表
- 微信公众号手机端无法打开,但是微信官方调试工具和电脑端可以打开
- 定制xdoclet 标签
- java web 开发问题总结 1 原创-胡志广
- 程序员们的那些神表情,有木有击中你^_^
- python-字典练习3 -数字重复统计
- 对坐标的曲线积分求做功_曲线积分与曲面积分(前篇 曲线积分-坐标曲线积分-格林公式)...
- VMware设置共享文件夹之后Ubuntu中看不到怎么办?
- github开发人员在七夕搞事情:remote: Support for password authentication was removed on August 13, 2021.
- 每日新闻 丨新突破:AI能提前十年预测心脏病,准确率高达90%
热门文章
- java中的if语句
- 深度学习算法2-SVM的原理
- 网络层ARP/RARP协议
- Java 1.10(以英里计的平均速度)假设一个跑步者45分30秒跑了14千米。编写一个程序显示以每小时多少英里为单位的平均速度值。(注意,1英里约等于1.6千米。)
- csdn服务器给我返回http 500, 呵呵哒!------后来才知道, 每天最多写5篇, 我晕啊, 能给个友好点的提示不?
- 值得收藏的十个销售类网站(整理),给想做销售的人用
- react高阶组件的使用
- 浅谈镜头滤镜的类型及作用
- python日历方法
- 6-1 图书列表 (25 分)