On Fri, Dec 3, 2010 at 10:53 AM, Stefan Behnel <[email protected]> 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 > same name as the Python visible attributes, this introduces behaviour that > diverges from CPython's own behaviour. Making these fields mutable means > that you will get an exception when you modify the field in CPython but a > straight value update after you compiled your code. > > Would it be so bad to require > > c.cval.real = 3.3 > > instead of > > c.real = 3.3 > > if you *really* think you must change the value of an immutable complex > object? > > BTW, the main reason why I implemented was the (mutable) slice type, not so > much the complex type.
Of course we have exactly the same asymmetry for the read-only attributes of typed cdef classes. - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
