Robert Bradshaw wrote:
> On May 20, 2009, at 10:10 PM, Dag Sverre Seljebotn wrote:
> 
>> Robert wrote:
>>> According to the Python/C API, returning -1 from __hash__ is the way
>>> to signal an error. Of course, if one returns -1 manually one gets a
>>> runtime error that an error was raised without setting the exception.
>>> We could document this, but it's still kind of obscure.
>>>
>>> In Python one can do
>>>
>>> sage: class A:
>>> ....:     def __hash__(self):
>>> ....:         return int(-1)
>>> ....:
>>> sage: hash(A())
>>> -2
>>>
>>> Should we just check for -1 and return -2 for cdef class __hash__
>>> functions as well?
>> +1, but IMO it should wait until the rest of the operators have been
>> discussed for alignment with Python as well.
>>
>> For instance I'd like to propose:
>> - Introduce "__cadd__" to take over for the current "__add__"
>> - Create a script which can convert source files (e.g. in Sage) and do
>> this change automatically (since the change below will slow things  
>> down)
>> - Make __cadd__ by default do something like this:
>>
>> cdef class MyClass:
>>     def __cadd__(a, b):
>>         # body inserted if __add__ is implemented
>>         if isinstance(a, MyClass):
>>             return a.__add__(a, b)
>>         else:
>>             return b.__add__(b, a)
> 
> This is a much more invasive change--I think the hash function is  
> safer because it turns what was a certain runtime error into  
> something reasonable.

Good point!

-- 
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to