On May 24, 2010, at 5:04 AM, Stefan Behnel wrote:

> luper rouch, 24.05.2010 13:43:
>> 2010/5/24 Stefan Behnel:
>>> luper rouch, 24.05.2010 00:07:
>>>> It seems there is a problem when attempting to access C members  
>>>> of an
>>>> extension type in special methods, such as __mul__ :
>>>
>>> you should avoid claiming to have found a bug just because you  
>>> didn't read
>>> the documentation.
>>>
>>> http://docs.cython.org/src/userguide/special_methods.html#arithmetic-methods
>>
>> Yes I certainly missed that in the docs, sorry.

Yep. We should probably try to feature that more prominently, as it's  
one of the most significant differences between cdef classes in Cython  
and pure Python and often trips newcomers up.

>> So, arithmetic
>> operators are some kind of static methods, is the following the right
>> way to implement them ?
>>
>>    def __mul__(op1, op2):
>>        if isinstance(op1, Quaternion):
>>            return from_c_obj((<Quaternion>op1).wrapped[0]  
>> *<float>op2)
>>        else:
>>            return from_c_obj((<Quaternion>op2).wrapped[0]  
>> *<float>op1)
>
> Assuming that you want a TypeError to be raised for any second  
> operand that
> can't be converted to a C float value, that should work.
>
> Note that float coercion will be handled by the Python runtime, so any
> (numeric or non-numeric) type that can convert itself into a float  
> value
> will work here. You can also write "float(op2)", which will do the  
> same thing.

Well, I would recommend using a cast over a call, unless you want

 >>> my_object * "1.23"

to work.

- Robert

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

Reply via email to