《Python 源码阅读》之 类型Type
2023-09-27 14:19:41 时间
py一切皆对象:
那么Type也是个对象。对象类型叫PyTypeObject
demo
>>> a = 1
>>> a
1
>>> type(a)
<type 'int'> int类型
#等价的两个
>>> type(int) int类型的类型就是type
<type 'type'>
>>> type(type(a)) type(a)是int类型的类型就是type
<type 'type'>
#还是等价的两个
>>> type(type(int))
<type 'type'>
>>> type(type(type(a)))
<type 'type'>
# 1. int 的 类型 是`type`
>>> type(int)
<type 'type'>
# 2. type 的类型 还是`type`, 对应上面说明第二点
>>> type(type(int))
<type 'type'>
1. 首先, 定义一种类型对象叫PyTypeObject
定义
typedef struct _typeobject {
/* MARK: base, 注意, 是个变长对象*/
PyObject_VAR_HEAD
const char *tp_name; /* For printing, in format "<module>.<name>" */ //类型名
Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */ // 创建该类型对象时分配的内存空间大小
// 一堆方法定义, 函数和指针
/* Methods to implement standard operations */
printfunc tp_print;
hashfunc tp_hash;
/* Method suites for standard classes */
PyNumberMethods *tp_as_number; // 数值对象操作
PySequenceMethods *tp_as_sequence; // 序列对象操作
PyMappingMethods *tp_as_mapping; // 字典对象操作
// 一堆属性定义
....
} PyTypeObject;
说明
- PyObject_VAR_HEAD 变长对象
- const char *tp_name tp_name, 类型名字符串数组
- 所有Type对象都是PyTypeObject的”实例”:
- 比如int类型:PyType_Type/PyInt_Type
2. 然后, 用PyTypeObject初始化得到一个对象PyType_Type
定义
PyTypeObject PyType_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"type", /* tp_name */
sizeof(PyHeapTypeObject), /* tp_basicsize */
sizeof(PyMemberDef), /* tp_itemsize */
(destructor)type_dealloc, /* tp_dealloc */
// type对象的方法和属性初始化值
.....
};
说明
- tp_name
类型名, 这里是”type”- PyVarObject_HEAD_INIT(&PyType_Type, 0)
PyVarObject_HEAD_INIT, 这个方法在 Include/object.h中,
等价于
ob_refcnt = 1
*ob_type = &PyType_Type
ob_size = 0
即, PyType_Type的类型是其本身!
3. 再然后, 定义具体的类型, 这里以PyInt_Type为例子
定义
PyTypeObject PyInt_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"int",
sizeof(PyIntObject),
0,
// int类型的相关方法和属性值
....
(hashfunc)int_hash, /* tp_hash */
};
说明
- “int”
PyInt_Type的类型名是int- PyVarObject_HEAD_INIT(&PyType_Type, 0)
PyInt_Type的
*ob_type = &PyType_Type
使用
>>> type(1)
<type 'int'>
>>> type(type(1))
<type 'type'>
4. 最后, 生成一个整数对象int
定义
typedef struct {
PyObject_HEAD
long ob_ival;
} PyIntObject;
结构
- PyIntObject为整数类型
- 声明一个整数后得到整数对象
- 对象ob_type指向PyInt_type对象
=========================
总结:
- 一切都是对象 什么都是PyObject/PyVarObject对象
- PyType_Type / PyInt_Type / PyString_Type ….等
这些是类型对象
, 可以认为是同级, 都是PyTypeObject这种类型
的实例!- 虽然是同级,
但是其他PyXXX_Type, 其类型指向 PyType_Type
PyType_Type 的类型指向自己, 它是所有类型的类型
- PyTypeObject 是一个变长对象
- 每个object, 例如PyIntObject都属于一种
类型
object初始化时进行关联
相关文章
- 人工智能时代,应立即学习python
- python中注释的写法
- python链表的实现,有注释
- Python获取类属性及其它(vim看源码常用、__dict__)
- 【软件测试】python接口自动化测试编写脚本,资深测试总结方法,你的实用宝典......
- python 用透 “进程、线程、协程”
- 使用 Python+Selenium + 第三方库实现简单的web自动化测试框架 源码
- Python集合
- 【Python】自动发送邮件
- python 音频通道分离的源码实现
- 《Python游戏编程快速上手》——导读
- Python中的序列操作
- Python游戏开发之Dungeon Crawler 游戏源码大全
- Python 数据可视化解决方案之 集成 PyPlot 和 PySimpleGUI (教程含源码)
- Python Manim教程大全之 05 动画运行时间 动画方法 (教程含源码)
- (数据科学学习手札13)K-medoids聚类算法原理简介&Python与R的实现
- Python GUI教程之使用 Python 构建 Excel 数据输入应用程序(教程含源码)
- SQL Python教程之在 Python 环境中使用 SQL进校数据查询分析聚合过滤pandsql (教程含源码和数据集)
- 《Python数据可视化之matplotlib实践》 源码 第四篇 扩展 第十一章
- 《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第四章
- 《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第二章
- 神经网络常用激活函数对比:sigmoid VS sofmax(附python源码)
- Python学习---模拟微信网页登录180410
- 解决yum升级的问题“There was a problem importing one of the Python modules”
- 为什么学习Python及Python环境安装
- Python 框架 之 Django MVT 下的 T 的 模板简单介绍和使用
- Python 爬虫 之 爬虫的一些基本知识和基本操作(爬取视频、图片、获取网页源码等)整理
- linux 下配置 python源
- Python中变量在内存的存储与地址变化详解(深浅拷贝,值/引用传递、可变不可变数据类型)
- Python切片一文读懂: Python 切片操作的深层理解_详细图解
- gdb 使用python源码调试
- 【Python】排序你学废了吗,茴香豆有四种写法,排序有十种写法