我是靠谱客的博主 大胆音响,这篇文章主要介绍[Python源码学习]之数据类型,现在分享给大家,希望可以做个参考。

标准类型层次

根据Manual中The standard type hierarchy一节的类型信息,我们首先尝试列出一个表:

类型

对象类

PyNone_Type

PyObject

None

PyBool_Type

PyLongObject

NotImplemented

PyEllipsis_Type

PyObject

Ellipsis

PyLone_Type

PyLongObject

numbers.Integral

numbers.Number

PyFloat_Type

numbers.Real

PyComplex_Type

numbers.Complex

PyUnicode_Type

PyUnicodeObject

Strings

Sequences

PyTuple_Type

Tuples

PyBytes_Type

PyBytesObject

Bytes

PyList_Type

Lists

PyByteArray_Type

Byte Arrays

PySet_Type

Sets

Set

PyTraceBack_Type

Frozen sets

PyDict_Type

Dictionaries

Mappings

PyFunction_Type

PyFunctionObject

用户自定义函数

Callable types

PyInstanceMethod_Type

PyInstanceMethodObject

实例的方法

生成器函数

PyCFunction_Type

PyCFunctionObject

内置函数

内置方法

类实例

PyModule_Type

Modules

Custom class

Class instances

I/O objects

PyCode_Type

Code objects

Internal types

PyFrame_Type

Frame objects

PyTraceBack_Type

Traceback objects

PySlice_Type

PySliceObject

Slice objects

PyStaticMethod_Type

staticmethod

Static method objects

PyClassMethod_Type

classmethod

Class method objects

刚开始接触这个东西,很多地方还不了解,以至于这个简单的表格都无法补全(留待以后慢慢完善)

Py***_Type

这些都是结构体PyTypeObject的实例,以PyLong_Type为例:

  • 变量声明在 Inlcude/longobject.h 中:

复制代码
1
PyAPI_DATA(PyTypeObject) PyLong_Type;
  • 注:PyAPI_DATA是一个宏,定义在 Include/pyport.h 中,用来隐藏动态库时平台的差异,片段如下:
    复制代码
    1
    2
    3
    4
    ... #define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE #define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE ...
  • 变量定义在 Object/longobject.c 中:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
PyTypeObject PyLong_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "int", /* tp_name */ offsetof(PyLongObject, ob_digit), /* tp_basicsize */ sizeof(digit), /* tp_itemsize */ long_dealloc, /* tp_dealloc */ ... &long_as_number, /* tp_as_number */ ... long_new, /* tp_new */ PyObject_Del, /* tp_free */ };
  • PyTypeObject本身也是一个PyObject,所以它也有自己的类型,这就是PyType_Type

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 */ ...
  • 类型信息在Python中通过type来获取

复制代码
1
2
3
4
5
6
>>> type(0L) <type 'long'> >>> type(long) <type 'type'> >>> type(type) <type 'type'>

参考

  • http://docs.python.org/reference/datamodel.html#types


最后

以上就是大胆音响最近收集整理的关于[Python源码学习]之数据类型的全部内容,更多相关[Python源码学习]之数据类型内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部