2

I would like to use an operator such as 'x' to give the cross-product of two vectors. Is there any way to add such an operator to Python 3?

user258279
  • 371
  • 4
  • 12
  • 1
    Is there a reason why a simple method like `vec1.cross(vec2)` isn't sufficient enough for you? It's not much harder than `vec1 x vec2` – Markus Meskanen Oct 10 '15 at 06:07
  • I don't want to dupe-hammer this, but look here http://stackoverflow.com/questions/932328/python-defining-my-own-operators – wim Oct 10 '15 at 06:33
  • 1
    If you are on python 3.5+, you might like to use `__matmul__`, see [PEP-0465](http://legacy.python.org/dev/peps/pep-0465/) – wim Oct 10 '15 at 06:36
  • @Markus Meskanen If we didn't have handy operators we'd be writing two.plus(two) == 4. It's easier to follow calculations if the code resembles the corresponding mathematic expressions. – user258279 Oct 10 '15 at 07:38

1 Answers1

1

You cannot supplement Python's set of operators and statements directly in the Python code. However, you can write a wrapper that uses Python's language services to write a Pythonesque DSL which includes the operators you want.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358