前言:

Python在2.2和3.0之间,把继承了object的类叫做新式类,如果我们定义了一个类,他没有继承object,则不是新式类,则没有__class__,__bases__等属性,而用type()函数查看他的类型,不是type类,而是classobj类。在py3后,默认所有的类都继承object. 我们接下来讨论的,是新式类

1 对象就是实例,实例就是对象

2.查看对象属于哪个类,用它的__class__属性查看,或者用内置函数type()

3.查看类的父类是什么,用它的__bases__属性查看

2.a = 'sss',那么a是str类的实例,或者说对象,python一切皆对象,那么str类也是一个对象,只是他不是普通的像a一样的对象,而是类对象。那么他又是哪个类的实例呢?回答:他是type类的实例。而type类又是属于哪个类的实例那?答案是,他是他自己的实例。

>>> a = 'sss'
>>> type(a)
<type 'str'>
>>> type(str)
<type 'type'>
>>> str.__class__
<type 'type'>
>>> type(type)
<type 'type'>

我们把type类称之为元类(MetaClass),即类的类。他是所有类的类,包括他自己。

5。str类的父类是basestring,basestring的父类是object,object的父类是什么?答案是没有。object是除他自己的一切类的最终父类(包括type类)。

新式类,经典类

以下转载自http://www.cnblogs.com/limuyuan/p/why-python-extend-object-class.html

自学Learn Python The Hard Way看到了这个问题,楼上各位回答讲真我是看的一头雾水。。

然后去stackoverflow搜索了一下,结合官方文档,说一下我自己的理解:

2 PEPs 252 and 253: Type and Class Changes

First, you should know that Python 2.2 really has two kinds of classes: classic or old-style classes, and new-style classes. The old-style class model is exactly the same as the class model in earlier versions of Python. All the new features described in this section apply only to new-style classes. This divergence isn't intended to last forever; eventually old-style classes will be dropped, possibly in Python 3.0.
So how do you define a new-style class? You do it by subclassing an existing new-style class. Most of Python's built-in types, such as integers, lists, dictionaries, and even files, are new-style classes now. A new-style class named object, the base class for all built-in types, has also been added so if no built-in type is suitable, you can just subclass object:

其实这里已经说得很清楚,Python 2.2有两种类:旧式类、新式类。旧式类是什么样的暂时不用管,只要记住,以后都用并且只用新式类,就对了。

那么怎么来声明或定义(define)一个新式类呢?做法就是,从现有的新式类中创建子类。

大多数的Python的内建类型(built-in type),比如整型(integers),列表(lists),字典(dictionaries),甚至文件(files),现在都是新式类了。我们也添加了一个叫object的新式类,作为所有内建类型的基类(base class),所以如果没有适合的内建类型,从object创建子类就好了:

class C(object):
def __init__ (self):
...
...

所以现在明白了吗?object只是从Python 2.2开始被引入的一个新式类(new-style class),作用是所有内建类型(built-in types)的基类(base class)

以下引用知乎:

作者:邹冲
链接:https://www.zhihu.com/question/19754936/answer/202650790
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

在 Python 2.7 里面新式类和经典类在多继承方面会有差异:

class A:
def foo(self):
print('called A.foo()') class B(A):
pass class C(A):
def foo(self):
print('called C.foo()') class D(B, C):
pass if __name__ == '__main__':
d = D()
d.foo()

B、C 是 A 的子类,D 多继承了 B、C 两个类,其中 C 重写了 A 中的 foo() 方法。

如果 A 是经典类(如上代码),当调用 D 的实例的 foo() 方法时,Python 会按照深度优先的方法去搜索 foo() ,路径是 B-A-C ,执行的是 A 中的 foo() ;

如果 A 是新式类,当调用 D 的实例的 foo() 方法时,Python 会按照广度优先的方法去搜索 foo() ,路径是 B-C-A ,执行的是 C 中的 foo() 。

因为 D 是直接继承 C 的,从逻辑上说,执行 C 中的 foo() 更加合理,因此新式类对多继承的处理更为合乎逻辑。

在 Python 3.x 中 所有类都是新式类, D 实例中的 foo() 都会执行 C 中的 foo() 。但是在 Python 2.7 中这种差异仍然存在,因此还是推荐使用新式类,要继承 object 类。

Python2的object和type的更多相关文章

  1. PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  2. PHP json_decode object时报错Cannot use object of type stdClass as array

    PHP json_decode object时报错Cannot use object of type stdClass as array php再调用json_decode从字符串对象生成json对象 ...

  3. MyBatis Generator报错:Cannot instantiate object of type

    [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate ( ...

  4. spring boot 之 错误:SpelEvaluationException: EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap'

    这个错误我也见过很多次了,今天终于理解了其出现的原因. 错误是这样的: 2017-11-23 18:05:39.504 ERROR 4092 --- [nio-8080-exec-3] o.a.c.c ...

  5. Unable to cast object of type 'System.Int32' to type 'System.Array'.

    x 入职了新公司.最近比较忙...一看博客...更新频率明显少了...罪过罪过... 新公司用ASP.NET MVC 遇上一个错误: Unable to cast object of type 'Sy ...

  6. iwebshop (: Cannot use object of type stdClass as array in)

    今天在PHP输出一个二维数组的时候,出现了“Fatal error: Cannot use object of type stdClass as array in……”. 这个二维数组是这样的: Ar ...

  7. celery 4.1下报kombu.exceptions.EncodeError: Object of type 'bytes' is not JSON serializable 处理方式

    #python代码如下 from celery import Celeryimport subprocess app = Celery('tasks', broker='redis://localho ...

  8. Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化

    Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化 一般原因为 序列化的对象 ...

  9. TypeError: Object of type 'int64' is not JSON serializable

    错误类型:TypeError: Object of type 'int64' is not JSON serializable 错误场景:对Numpy和Pandas结果进行json.dumps报错 错 ...

随机推荐

  1. 【SE】Week3 : 四则运算式生成评分工具Extension&Release Version(结对项目)

    Foreword 此次的结对项目终于告一段落,除了本身对软件开发的整体流程有了更深刻的了解外,更深刻的认识应该是结对编程对这一过程的促进作用. 在此想形式性但真心地啰嗦几句,十分感谢能端同学能够不厌其 ...

  2. Java编写准备数据源

    1.装饰设计模式 package com.itheima.ds; import java.sql.Array; import java.sql.Blob; import java.sql.Callab ...

  3. Confluence的简单安装以及与jira链接(Confluence不知道有没有破解)

    1. 前提是安装好了jira以及下载好了confluence的安装包 这里 jira的版本是 7.2.4 confluence的版本是6.8 2. 服务器上面有sqlserver数据库. 3. 为了便 ...

  4. 一本通1649【例 2】2^k 进制数

    1649:[例 2]2^k 进制数 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 原题来自:NOIP 2006 提高组 设 r 是个 2k 进制数,并满足以 ...

  5. BZOJ 3516 国王奇遇记加强版(乱推)

    题意 求\(\sum_{k=1}^{n}k^mm^k (n\leq1e9,m\leq1e3)\) 思路 在<>中有一个方法用来求和,称为摄动法. 我们考虑用摄动法来求这个和式,看能不能得到 ...

  6. gson 说明

    JSON对象格式 法兹测试仪测试案例编纂JavaScript对象表示法(JSON)格式的特殊字符转义,类型等,由于谷歌GSON是底层的JSON库处理类型的详细说明,请参阅到GSON文档的详细信息,请参 ...

  7. Java NIO 详解(一)

    一.基本概念描述 1.1 I/O简介 I/O即输入输出,是计算机与外界世界的一个借口.IO操作的实际主题是操作系统.在java编程中,一般使用流的方式来处理IO,所有的IO都被视作是单个字节的移动,通 ...

  8. Luogu4980 【模板】Polya定理(Polya定理+欧拉函数)

    对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n). 那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n). ...

  9. POJ 2584 T-Shirt Gumbo

    T-Shirt Gumbo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3689   Accepted: 1755 Des ...

  10. MT【170】裂项相消

    已知$a,b>0$证明:$\dfrac{1}{a+2b}+\dfrac{1}{a+4b}+\dfrac{1}{a+6b}<\dfrac{3}{\sqrt{(a+b)(a+7b)}}$ 证明 ...