类型对象 ¶
Perhaps one of the most important structures of the Python object system is the structure that defines a new type: the
PyTypeObject
structure. Type objects can be handled using any of the
PyObject_*
or
PyType_*
functions, but do not offer much that’s interesting to most Python applications. These objects are fundamental to how objects behave, so they are very important to the interpreter itself and to any extension module that implements new types.
Type objects are fairly large compared to most of the standard types. The reason for the size is that each type object stores a large number of values, mostly C function pointers, each of which implements a small part of the type’s functionality. The fields of the type object are examined in detail in this section. The fields will be described in the order in which they occur in the structure.
In addition to the following quick reference, the
范例
section provides at-a-glance insight into the meaning and use of
PyTypeObject
.
Quick Reference ¶
“tp slots” ¶
|
PyTypeObject Slot [ 1 ] |
special methods/attrs |
Info [ 2 ] |
||||
|---|---|---|---|---|---|---|
|
O |
T |
D |
I |
|||
|
<R>
|
const char * |
__name__ |
X |
X |
||
|
X |
X |
X |
||||
|
X |
X |
|||||
|
X |
X |
X |
||||
|
X |
X |
|||||
|
(
|
__getattribute__, __getattr__ |
G |
||||
|
(
|
__setattr__, __delattr__ |
G |
||||
|
% |
||||||
|
__repr__ |
X |
X |
X |
|||
|
% |
||||||
|
% |
||||||
|
% |
||||||
|
__hash__ |
X |
G |
||||
|
__call__ |
X |
X |
||||
|
__str__ |
X |
X |
||||
|
__getattribute__, __getattr__ |
X |
X |
G |
|||
|
__setattr__, __delattr__ |
X |
X |
G |
|||
|
% |
||||||
|
unsigned long |
X |
X |
? |
|||
|
const char * |
__doc__ |
X |
X |
|||
|
X |
G |
|||||
|
X |
G |
|||||
|
__lt__, __le__, __eq__, __ne__, __gt__, __ge__ |
X |
G |
||||
|
X |
? |
|||||
|
__iter__ |
X |
|||||
|
__next__ |
X |
|||||
|
|
X |
X |
||||
|
|
X |
|||||
|
|
X |
X |
||||
|
__base__ |
X |
|||||
|
|
__dict__ |
? |
||||
|
__get__ |
X |
|||||
|
__set__, __delete__ |
X |
|||||
|
(
|
X |
? |
||||
|
__init__ |
X |
X |
X |
|||
|
X |
? |
? |
||||
|
__new__ |
X |
X |
? |
? |
||
|
X |
X |
? |
? |
|||
|
X |
X |
|||||
|
<
|
|
__bases__ |
~ |
|||
|
<
|
|
__mro__ |
~ |
|||
|
[
|
|
|||||
|
[
|
void * |
__subclasses__ |
||||
|
[
|
|
|||||
|
(
|
||||||
|
[
|
无符号 int |
|||||
|
__del__ |
X |
|||||
|
[
|
unsigned char |
|||||