What Does __flags__ In Python Type Used For
Solution 1:
__flags__
is a wrapper, to access CPython type object structure member tp_flags
, constants used to compose this flag defined in object.h, following is quoted from the source:
Type flags (tp_flags) These flags are used to extend the type structure in a backwards-compatible fashion. Extensions can use the flags to indicate (and test) when a given type structure contains a new feature. The Python core will use these when introducing new functionality between major revisions (to avoid mid-version changes in the PYTHON_API_VERSION).
See more detail on python document on tp_flags
.
But interesting thing is that class.flags & _HEAPTYPE is always 0 when the class is a python internal type. And the result will be 1 when class is a subclass of python internal type.
Subclass of python built-in type, like other user defined type, allocated on heap with PyType_GenericAlloc()
.
to break down type.__flags__
:
import re
defflags_to_name(type_obj):
tp_flag_consts = {}
withopen('/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 inenumerate(bin_str) if c == '1')
print(flags_to_name(type))
yields:
Py_TPFLAGS_BASETYPE, Py_TPFLAGS_READY, Py_TPFLAGS_HAVE_GC, Py_TPFLAGS_HAVE_VERSION_TAG, Py_TPFLAGS_VALID_VERSION_TAG, Py_TPFLAGS_TYPE_SUBCLASS
Solution 2:
__flags__
is to be viewed as binary, if you want to see the flags.
These type flags are defined here in Python's source code.
Post a Comment for "What Does __flags__ In Python Type Used For"