【Python深入】Python中继承object和不继承object的区别
python中定义class的时候,有object和没有object的不同?例如:
class Solution(object):
class Solution():
这俩的区别在于——————
在python2.x中,通过分别继承自object和不继承object定义不同的类,之后通过dir()和type分别查看该类的所有方法和类型:
>>> class test(object):
... pass
...
>>> dir(test)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '_
_init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size
of__', '__str__', '__subclasshook__', '__weakref__']
>>> type(test)
<type 'type'>
>>> class test2():
... pass
...
>>> dir(test2)
['__doc__', '__module__']
>>> type(test2)
<type 'classobj'>
在3.x中:两者是一致的,因为在3.x中,默认继承就是object了
>>> class test(object):
pass >>> class test2():
pass >>> type(test) <class 'type'> >>> type(test2) <class 'type'> >>> dir(test) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] >>> dir(test2) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
【Python深入】Python中继承object和不继承object的区别的更多相关文章
- Python工程文件中的名词解释---Module与Package的区别
当我们在已有的Python工程文件中创建新的内容是,通常会有两种类型文件供你选择---Module和Package,对于初学者来说会搞不清楚这两种文件直接的关系.这里就来解释一下这两者之间的关系. M ...
- python time包中的time.time()和time.clock()的区别
在统计python代码 执行速度时要使用到time包,在查找相关函数时有time.time()和time.clock()两个函数可供选择.而两者是有区别的: cpu 的运行机制:cpu是多任务的,例如 ...
- python 读写文件中 w与wt ; r与rt 的区别
w,r,wt,rt都是python里面文件操作的模式.w是写模式,r是读模式.t是windows平台特有的所谓text mode(文本模式),区别在于会自动识别windows平台的换行符.类Unix平 ...
- python定义类()中写object和不写的区别
这里需要说明一下: python3中,类定义默认继承object,所以写不写没有区别 但在python2中,并不是这样 所以此内容是针对python2的,当然python3默认继承,不代表我们就傻乎乎 ...
- Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)
Python 在子类中调用父类方法详解(单继承.多层继承.多重继承) by:授客 QQ:1033553122 测试环境: win7 64位 Python版本:Python 3.3.5 代码实践 ...
- 'dict_values' object does not support indexing, Python字典dict中由value查key
Python字典dict中由value查key 众所周知,字典dict最大的好处就是查找或插入的速度极快,并且不想列表list一样,随着key的增加越来越复杂.但是dict需要占用较大的内存空间,换句 ...
- 关于Python中的类普通继承与super函数继承
关于Python中的类普通继承与super函数继承 1.super只能用于新式类 2.多重继承super可以保公共父类仅被执行一次 一.首先看下普通继承的写法 二.再看看super继承的写法 参考链接 ...
- python学习第十五天 -面向对象之继承和多态
大家都知道面向对象的三大特性:封装,继承,多态.封装特性在上一章节已经讲解过.这一章节主要讲解继承和多态. 继承: 当定义一个类的时候,可以从现有的类进行继承.那么新定义的类可以称为子类,被继承的现有 ...
- [修]python普通继承方式和super继承方式
[转]python普通继承方式和super继承方式 原文出自:http://www.360doc.com/content/13/0306/15/9934052_269664772.shtml 原文的错 ...
随机推荐
- 【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf
========7.SpringBoot常用Starter介绍和整合模板引擎Freemaker.thymeleaf ========================= 1.SpringBoot Sta ...
- What’s New In GRANDstack?
转自:https://blog.grandstack.io/whats-new-in-grandstack-310c067fea4a There’s been a lot of activity in ...
- C# ZipHelper C#公共类 -- ZipArchive实现压缩和解压
从网上找来个ZipArchive来压缩和解压缩的类,供参考吧 /******************************************************************** ...
- Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found
Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found 网上大部分文章都是说在bios开启vt-x支持等.这里 ...
- erlang遍历目录
{ok, Cwd} = file:get_cwd(). Filelist = filelib:fold_files( Cwd, ".*", true, fun(File, Acc) ...
- 位置式PID与增量式PID
//位置式PID float Kp; float Ki; float Kd; float eSum,e0,e1; float pid_control(float now,float target) { ...
- HashMap与Hashtable
1. 1) Hashtable继承自Dictionary类,HashMap是map接口的一个实现类(继承自AbstractMap抽象类) 2) 在HashMap中,null可以 ...
- kettle大数据量读写mysql性能优化
修改kettleDB连接设置 1. 增加批量写的速度:useServerPrepStmts=false rewriteBatchedStatements=true useCompressio ...
- Python全栈开发记录_第二篇(文件操作及三级菜单栏增删改查)
python3文件读写操作(本篇代码大约100行) f = open(xxx.txt, "r", encoding="utf-8") 不写“r”(只读)默认是只 ...
- 201. Spring Boot JNDI:Spring Boot中怎么玩JNDI
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...