[Cython] a = b = (cdef) cval
Robert Bradshaw
robertwb at math.washington.edu
Sat Apr 12 02:22:03 CEST 2008
On Apr 11, 2008, at 4:58 PM, Lisandro Dalcin wrote:
> It seems the code generated by
>
> cdef MyClass cval = MyClass()
>
> a = b = cval
>
> is not equivalent in semantics to Python, 'a is b' should be true.
Actually it follows Python semantics exactly. It does not follow C
semantics (which treats b = cval as an expression). There are many
fewer cases in Python where code is executed on assignment, but
unpacking is one of them. Here is a demonstration:
class Unpackable:
def __iter__(self):
try:
self.n += 1
print "unpacking", self.n
return iter([self.n])
except AttributeError:
self.n = 0
return iter(self)
(a,) = (b,) = (c,) = Unpackable(); print a, b, c
unpacking 1
unpacking 2
unpacking 3
1 2 3
> I acctually did this
>
> cdef int cval = 10
> a = b = cval
>
> then 'a is b' is true, because of the internal Python cache for
> small integers.
Yep.
I am grateful for your attention to details, I am sure it will help
make Cython better.
- Robert
More information about the Cython-dev
mailing list