在阅读关于类型统一的内容时,我偶然发现内置类型有method_descriptors和builtin_function_or_methods而不是methods和functions,为什么呢?
>>> list.append
<method 'append' of 'list' objects>
>>> type(list.append)
<class 'method_descriptor'>
>>> [].append
<built-in method append of list object at 0x7f0c4214aef0>
>>> type([].append)
<class 'builtin_function_or_method'>
>>> class A(list):
... def append(self): pass
...
>>> A.append
<function A.append at 0x7f0c42168dd0>
>>> type(A.append)
<class 'function'>
>>> A().append
<bound method A.append of []>
>>> type(A().append)
<class 'method'>
Run Code Online (Sandbox Code Playgroud)
没有充分的理由class A将子列表子类化,我只是想表明类型不同.
我怎样才能做到这一点:
cdef class Tree:
cdef object key
cdef Tree left
cdef Tree right
cdef PyObject** find(self, key):
# get the address of self
# return &self
# return &(<PyObject*>self)
Run Code Online (Sandbox Code Playgroud)
&self失败并显示Cannot take address of Python variable.&(<PyObject*>self)失败Taking address of non-lvalue,并且我不确定这self实际上是一个PyObject*.我正在阅读getaddrinfo的手册页并尝试遵循示例代码,但它不能编译:
// test.c
#include <netdb.h>
void f() {
struct addrinfo t;
}
Run Code Online (Sandbox Code Playgroud)
两个都有clang:
$ clang -std=c11 test.c
test.c:10:21: error: variable has incomplete type 'struct addrinfo'
struct addrinfo t;
^
test.c:10:12: note: forward declaration of 'struct addrinfo'
struct addrinfo t;
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
和gcc:
$ gcc -std=c11 test.c
test.c: In function ‘f’:
test.c:10:21: error: storage size of ‘t’ isn’t known
struct addrinfo t;
^
Run Code Online (Sandbox Code Playgroud)
经过一番挖掘后,我发现glibc头文件有一个预处理器指令,可以"保护"(这是正确的术语吗?)struct来定义,所以我想知道:
#ifdef __USE_XOPEN2K而其他人不使用?什么用于?为什么它从__USE_POSIX改变了?我试图用一个向量作为缓冲这样的,我想知道是否有一种方法可以采取从载体片没有增长它,所以像这样的工作,代码:
fn example(r: Read) {
let buffer: Vec<u8> = Vec::with_capacity(1024);
let slice: &mut[u8] = &mut buffer;
r.read(slice); // doesnt work since the slice has length zero
}
Run Code Online (Sandbox Code Playgroud)