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. 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. > And for intransitive operations like __div__ return NotImplemted in > the reversed case : > > def __div__(op1, op2): > if isinstance(op1, Quaternion): > return from_c_obj((<Quaternion>op1).wrapped[0] /<float>op2) > else: > return NotImplemented Looks ok. Note that the cython-users mailing list would have been more appropriate for this thread. Stefan _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
