`
rockkyle
  • 浏览: 10357 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

python object和type

 
阅读更多

一、object和type的关系

首先了解2条规则

1.若x是A的一个实例,且A是B的子类,那么x也是B的一个实例

2.若B是M的实例,且A是B的子类,那么A也是M的一个实例

首先type为object的子类,(A=type,B=object)

>>> issubclass(type,object)
True

其次object是type的实例(x=object,A=type)

>>> isinstance(object,type)
True

 规则1得出object是object的实例(B=object,M=object)

>>> isinstance(object,object)
True

 规则2得出type是object的实例

>>> isinstance(type,object)
True

 同样可以得到type是type自己实例

 

>>> isinstance(type,type)
True

 PS:python中所有类都是自己本身的子类

>>> class A:
...     pass
... 
>>> issubclass(A,A)
True

 二、object和python中的对象和类

python中的所有对象都是object的实例,所有类都是object的子类(除了旧式类)

>>> class A:
...     pass
... 
>>> type(A)
<type 'classobj'>
>>> isinstance(A,object)
True
>>> issubclass(A,object)
False

 PS:新式类与旧式类的区别

声明上:

>>> class A(object):
...     pass
... 
>>> class A:
...     pass
... 

  规则:

旧式类是广度优先

class A:
        value="a"
class B(A):
        pass
class C(A):
        value="c"
class D(B,C):
        pass
d=D()
print d.value

结果为“a",搜索基类为深度优先

新式类:

class A(object):
        value="a"
class B(A):
        pass
class C(A):
        value="c"
class D(B,C):
        pass
d=D()
print d.value

 结果为“c",广度优先

PS:p y3.0中2种声明都为新式类,若要声明旧式类,括号中需制定为旧式类

分享到:
评论

相关推荐

    TypeError object of type ‘type’ has no len()—Python报错问题:

    翻译过来是类型为“type”的TypeError对象没有len(),我报错的代码是: #coding=utf-8 print(请输入一个字符串:) a = input('') #回文的长度至少为2 if len(str) &lt; 2: print('请不要输入空字符串!') a = ...

    Python:type、object、class与内置类型实例

    Python:type、object、class Python: 一切为对象 &gt;&gt;&gt; a = 1 &gt;&gt;&gt; type(a) &gt;&gt;&gt; type(int) type =&gt; int =&gt; 1 type =&gt; class =&gt; obj type是个类,生成的类也是对象,生成的实例是对象 &gt;&gt;&gt;class Student: &gt;&gt;&gt; pass &gt;...

    详解Python中的type和object

    主要介绍了Python中type和object的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

    Python的类型和对象

    讲述Python的type, class和对象之间的关系, Python 的对象模型,翻译自Python type and object

    Python object类中的特殊方法代码讲解

    在本篇文章里小编给各位整理了关于Python object类中的特殊方法代码讲解,需要的朋友们可以参考下。 python版本:3.8 class object: """ The most base type """ # del obj.xxx或delattr(obj,'xxx')时被调用,删除...

    python入门教程大全.pdf 精心整理

    1.[python入门教程]将excel导入到...12.[python入门教程]Python3中的type和object用法 13.[python入门教程]python3中bs4.FeatureNotFound提示报错的处理办法 14.[python入门教程]python安装requests的步骤 ........

    Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法

    Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法 TypeError: ‘NoneType’ object is not iterable 这个错误提示一般发生在将None赋给多个值时。 def myprocess(): a == b if a != b: ...

    python pyinstaller3.4 win10补丁,修复环境变量错误

    Python3.7 在使用pyinstaller3.4打包发布可执行文件时报错:TypeError: expected str, bytes or os.PathLike object, not NoneType pyinstall。 下载后替换项目根目录下\venv\Lib\site-packages\PyInstaller\depend\...

    fasterRCNN python36

    Tensorflow版 fasterrcnn的cpython_bbox 适用于 python36 win64位的机器

    python源码剖析(样章)

    在图的右边,是 Python 的运行时环境,包括对象/类型系统(Object/Type structures),内存分配器(Memory Allocator)和运行时状态(Current State of Python)。运行时状态维护了解释器在执行字节码时在不同的状态...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...

    Python3爬楼梯算法示例

    本文实例讲述了Python3爬楼梯算法。分享给大家供大家参考,具体如下: 假设你正在爬楼梯。需要 n 步你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数...

    Get Programming - Learn to code with Python.epub

    Lesson 31 - Creating a class for an object type Lesson 32 - Working with your own object types Lesson 33 - Customizing classes Lesson 34 - Capstone project: card game UNIT 8 - USING LIBRARIES TO ...

    msgpack-python-0.4.2.tar

    *raw* was bytes or string type like Python 2's ``str``. To distinguish string and bytes, msgpack 2.0 adds *bin*. It is non-string binary like Python 3's ``bytes``. To use *bin* type for packing ``...

    Python库 | python-didl-lite-1.1.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:python-didl-lite-1.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    AttributeError: ‘NoneType’ object has no attribute ‘children’ 错误

    在运行嵩天老师python爬虫课中单元6中的实例“中国大学排名爬虫”会出现如下图错误:AttributeError: ‘NoneType’ object has no attribute ‘children’ 意思是 ‘NoneType’ 对象没有属性 ‘children’ ,这个...

    python输出数据类型.md

    在Python中,要输出数据类型的语法通常使用type()函数。type()函数的作用是返回输入对象的数据类型。 type(object) 其中,object是要检查其数据类型的对象,可以是任何数据类型的对象,例如整数、字符串、列表、元组...

Global site tag (gtag.js) - Google Analytics