Skip to content

Conversation

@bauen1
Copy link
Contributor

@bauen1 bauen1 commented Apr 13, 2019

The current Implementation of vectors raises an exception in __mul__ when you try to operate on a non-vector type.
This can be quite annoying, because even if for example Quaternion implements __rmul__ it will never be called when the vector is on the left side of the equation and instead error out.
Furthermore: https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types specifies that (some) operators should return NotImplemented.

# Does not work, should work
v = vector(0, 0, 0) * quaternion(0, 0, 0, 0)
# Does work, should work
v = quaternion(0,0,0,0) * vector(0,0,0,0)

Both the python and cython implementations have been tested (existing physics project that makes heavy use of vectors didn't break)

According to https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
operators should return NotImplemented
This also allows to use vpythons vectors for custom classes
@BruceSherwood BruceSherwood merged commit e3ee2b5 into BruceSherwood:master Apr 14, 2019
@BruceSherwood
Copy link
Owner

Many thanks for this excellent improvement. You're right; this is the way the vector class should work.

@bauen1 bauen1 deleted the fix_vector_operators branch December 18, 2019 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants