__flags__
是一个包装,
to access CPython type object structure member
tp_flags
,用于组成此标志的常数
defined in object.h
,以下引自消息来源:
类型标志(tp_标志)
这些标志用于以向后兼容的方式扩展类型结构
时尚扩展可以使用标志来指示(和测试)给定的
类型结构包含一个新功能。Python核心将在以下情况下使用它们:
在主要版本之间引入新功能(以避免中间版本
PYTHON_API_版本中的更改)。
查看更多详细信息
python document on
tp_flags
.
但有趣的是
班
.
旗帜
&_当
班
是python内部类型。当
班
是python内部类型的子类。
python内置类型的子类,与其他用户定义类型一样,在堆上分配
PyType_GenericAlloc()
.
崩溃
type.__flags__
:
import re
def flags_to_name(type_obj):
tp_flag_consts = {}
with open('/path/to/Include/object.h') as f:
for l in f:
m = re.search(r'^#define (Py_TPFLAGS_\w+)\s+\(.+?<< (\d+)\)', l.strip())
if m:
tp_flag_consts[int(m.group(2))] = m.group(1)
bin_str = bin(type_obj.__flags__)[2:][::-1]
return ', '.join(tp_flag_consts[n] for n, c in enumerate(bin_str) if c == '1')
print(flags_to_name(type))
产量:
Py_TPFLAGS_BASETYPE, Py_TPFLAGS_READY, Py_TPFLAGS_HAVE_GC, Py_TPFLAGS_HAVE_VERSION_TAG, Py_TPFLAGS_VALID_VERSION_TAG, Py_TPFLAGS_TYPE_SUBCLASS