On Fri, Dec 3, 2010 at 11:04 AM, Dag Sverre Seljebotn <[email protected]> wrote: > On 12/03/2010 07:56 PM, Dag Sverre Seljebotn wrote: >> On 12/03/2010 07:53 PM, Stefan Behnel wrote: >> >>> Lisandro Dalcin, 03.12.2010 19:39: >>> >>> >>>> On 3 December 2010 15:10, Stefan Behnel wrote: >>>> >>>> >>>>> I just implemented C access to the fields of CPython's builtin types like >>>>> this: >>>>> >>>>> cdef complex c = 1+2j >>>>> c.real, c.imag = 10, 20 >>>>> print (c.real, c.imag) >>>>> >>>>> and then noticed that "real" and "imag" are actually read-only in Python. >>>>> So I wonder if we should disallow the above (i.e. raise a compiler error >>>>> during type analysis) and instead only allow this: >>>>> >>>>> cdef complex c = 1+2j >>>>> cval =&c.cval >>>>> cval.real, cval.imag = 10, 20 >>>>> print (c.real, c.imag) >>>>> >>>>> (which also works now, BTW). >>>>> >>>>> Opinions? >>>>> >>>>> >>>> No! No! No! >>>> >>>> In Python, you cannot assign to real or imag simply because the >>>> complex is immutable (like Python ints and floats). For C types, >>>> immutability does not apply! >>>> >>>> >>> I understand that. However, I hope that you are talking about the builtin >>> Python complex type just like I am. Given that these two fields carry the >>> >>> >> No. If you do "cdef complex c", what you have is a C complex type. >> >> How would you treat "cdef float complex c"? Python has no object for >> single precision complex. >> > > Ah...sorry, seems me and Lisandro skipped your first sentence. (And I > was wrong, indeed "cdef complex c" is undefined in current Cython and > available for this purpose.) > > YES, for access to something that is an object, I do support immutability. > > However, I do have a much bigger problem with the naming here. I find > this very little user-friendly: > > cdef complex x # x is Python object > cdef double complex x # same precision but in C > cdef float complex x # single precision in C > > I really do feel that "int" has kind of set the precedent here, and that > if only "complex" is made available it should be as "double complex" or > similar. > > We should find a consistent solution for this that works the same for > complex and int and bool... > > cdef __builtin__.int x > cdef __builtin__.complex x > > or > > cdef object int x > cdef object complex x > > or something like that.
+1. We can already cimport these from cpython. object complex is a bit odd because it makes it feel like the real and imaginary components are objects. - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
