Consider, for example, quaternion math ( https://en.wikipedia.org/wiki/Quaternion ).
Obviously, I can build a class that defines a new quaternion
a = quaternion(1, 2, 3, 4)
print(a)
However, one cannot fail to notice that the native data type complexcan be defined much better using
a = 1 + 2j
type(a)
The syntax 1 + 2jis somehow captured to create a built-in class of complex (1,2) objects.
So can I do the same? Is there a way to capture simple direct syntax, for example a = 1 + 2i + 3j + 4k, and translate it into mine a = quartenion(1, 2, 3, 4)in a way similar to the built-in ones complex?
source
share