0

So I am developing a class for vectorial calculations, and I overwrote the __mul__(self, b) function to do a Scalarproduct. Now whenever I write A * B it calculates as I want it to. So I would like to do the same with an x for the Crossproduct. Sadly there is no default x operator in python, it would probably just annoy you. But is there a way to create your own operator which ONLY works for my own class, but can otherwise be used in a code aswell (as a variable definition I mean)? Or maybe define *** as an operator?

3
  • 2
    In brief: No, you cannot define your own operators in Python. Commented Jan 25, 2018 at 6:37
  • As DYZ's pointer question suggests, you may want to check out this hacky approach: code.activestate.com/recipes/384122-infix-operators Commented Jan 25, 2018 at 6:53
  • In recent versions of Python you could override the __matmul__ method, which is associated with the @ operator. It's supposed to be for matrix multiplication, but that's not a hard & fast rule. Commented Jan 25, 2018 at 8:35

1 Answer 1

0

Instead of just define an operator, you can write your own interpreter for your own language inside python. Basically, you need to create 4 modules

  • parser

  • environment

  • eval

  • repl

Then you can program with your own language within python.

read Peter Novig's article for a detailed example of a python LISP interpreter

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.