[Cython] CEP 507/513
Stefan Behnel
stefan_ml at behnel.de
Sun Apr 13 17:13:08 CEST 2008
Hi,
Robert Bradshaw wrote:
> The original proposal was that some Python objects (e.g. list, tuple,
> dict, str) would be known by the compiler and optimized accordingly
> (e.g. using the appropriate macros for indexing). One thing that is
> radical about this proposal (and why a pxd file wouldn't suffice) is
> that something declared "cdef list" would have to be *exactly* a
> list, not a subclass of list. In this case I think it is worth it
> because there are enormous gains to be made and 99% of Python lists
> are actually just lists. These would be exactly like C types which
> have no hierarchy.
I think this is the right way to do it. Due to the usual naming convention of
having extension types start with an upper-case letter, these declarations
even look like C types - and the only reason I could imagine why you would
declare a variable "cdef list" is to avoid having to write
PyList_Append(l, x)
print PyList_GetItem(l, i)
instead of
l.append(x)
print l[i]
You wouldn't gain *anything* by declaring a variable "cdef list" that holds a
subtype of list.
BTW, at least PyList_Append() seems to work just fine for subtypes:
------------------------------------
cdef extern from "Python.h":
ctypedef class __builtin__.list [object PyListObject]:
pass
cdef int PyList_Append(object l, object obj) except -1
cdef class csublist(list):
def append(l, x):
print "APPEND", x
l = csublist()
PyList_Append(l, 1)
print l
import __builtin__
class pysublist(__builtin__.list):
def append(l, x):
print "APPEND", x
l = pysublist()
PyList_Append(l, 1)
print l
------------------------------------
The "APPEND"s are not printed (as expected), so the output is:
------------------------------------
[1]
[1]
------------------------------------
Stefan
More information about the Cython-dev
mailing list