On Apr 11, 2008, at 5:33 PM, Lisandro Dalcin wrote:
> Ok and fine, jut just to be sure, if I do
>
> cdef int val = 123456789 # large int
> a = b = val  # 'a' and 'b' are then python integers
> assert a is b # test for object identity
>
> the the assert should or should not pass in Cython? Should or should
> not Cython behave like Python in the following case::
>
> In [1]: a = b = 12345678
>
> In [2]: a is b
> Out[2]: True
>
> In [3]: a = 12345678
>
> In [4]: b = 12345678
>
> In [5]: a is b
> Out[5]: False

No, it should not be True in Cython--if conversion is needed it is  
executed at each assignment (as in my previous example). Of course  
one can't write that cython test case in Python to try it out.

As another example, consider.

cdef double a
cdef int b
a = b = float(1.5) # make it a Python object or otherwise the  
compiler will (fortunately) complain.
print a, b
1.5 1

Note that integer literals in Cython are not Python objects (for  
obvious speed reasons). In Python they are. This is a subtle point,  
but fortunately one rarely has to worry about it.


> On 4/11/08, Robert Bradshaw <[EMAIL PROTECTED]> wrote:
>> 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
>>
>>
>>
>
>
> -- 
> Lisandro Dalcín
> ---------------
> Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
> Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
> Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
> PTLC - Güemes 3450, (3000) Santa Fe, Argentina
> Tel/Fax: +54-(0)342-451.1594

_______________________________________________
Cython-dev mailing list
Cython-dev@codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to