我们称之为类型化内存视图并不是一个单一的类:它根据上下文(Cython代码,纯Python代码)在后台更改其标识。
那么让我们从
%%cython
cdef class Temp:
cdef double[::1] inp
在这里
double[::1] inp
是一种
__Pyx_memviewslice
typedef struct {
struct {{memview_struct_name}} *memview;
char *data;
Py_ssize_t shape[{{max_dims}}];
Py_ssize_t strides[{{max_dims}}];
Py_ssize_t suboffsets[{{max_dims}}];
} {{memviewslice_name}};
当我们打电话时会发生什么
id(self.inp)
? 明显地
id
是一个纯Python函数,因此必须从中创建一个新的临时Python对象(memoryview)
self.inp
(只能打电话
身份证件
)然后直接销毁。临时Python对象的创建是通过
__pyx_memoryview_fromslice
.
知道了这一点,就很容易解释为什么ID是相等的:尽管是不同的对象,但临时内存视图碰巧有相同的地址(因此也是相同的)
身份证件
Python中到处都有类似的场景,下面是
an example for method-objects
,甚至是更简单的:
class A:
pass
# the life times of temporary objects don't overlap, so the ids can be the equal
id(A())==id(A())
# output: True
# the life times of objects overlap, so the id cannot be equal
a,b=A(), A()
id(a)==id(b)
# output: False
简而言之,你的期望是一样的
身份证件
意味着同一个对象是错误的。只有当对象的生命周期重叠时,此假设才成立。