当前位置:首页 » python教程 » 正文

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

看: 605次  时间:2020-12-28  分类 : python教程

Python:type、object、class

Python: 一切为对象

>>> a = 1
>>> type(a)
<class'int'>
>>> type(int)
<class'type'>

type => int => 1

type => class => obj

type是个类,生成的类也是对象,生成的实例是对象

>>>class Student:
>>> pass
>>>
>>>stu = Student()
>>>type(stu)
__main__.Student
>>>Student.__base__
object

Student的基类是obj

>>>class MyStudent(Student):
>>> pass
>>>
>>>MyStudent.__base__
__main__.Student

MyStudent继承Student

MyStudent的基类是Student

object是最顶层的基类

type是个类,同时type也是个对象

>>> type.__base__
object
>>> type(object)
type
>>> object.__bases__
()

object是type的实例

type继承object

我们可以把这些分为3类:

第一类:type自成一类,type是自己的对象(可以实例化自己),type可以把所有变成他的对象

第二类:list、str、Student…类会继承object,list、str、Student…是类,同时也是type的对象。object是所有的基类(一切都继承object)

第三类:生成的对象

Python 内置类型

对象的三个特征:

身份

类型

身份:每个对象身份均不同

>>> a = 1
>>> id(a)
4333971504
>>> a = {}
>>>id(a)
4393125064

类型

None(全局只有一个)

数值

int

float

complex(复数)

bool

迭代类型

序列类型

list

bytes、bytearray、memoryview(二进制序列)

range

tuple

str

array

映射(dict)

集合

set

frozenset

上下文管理类型(with)

其他

模块类型

class和实例

函数类型

方法类型

代码类型

object对象

type类型

ellipsis类型

notimplemented类对象

None类型:Python在程序启动的时候会生成一个None对象

>>> a = None
>>> b = None
>>> id(a) == id(b)
True

以上这篇Python:type、object、class与内置类型实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!