Search code examples
pythondynamic-languages

Overwriting default behavior for python operators


I know that to alter the default behavior of operators in python you can override some default methods like __add__ or __sub__ for + and -, but didn't find anything to override the behavior of the and and or keywords, while there are some for the bitwise operators &, |: respectively __and__ and __or__.

Do you know if there are hooks for these keywords also? I know it's strange to override the default behavior of and & or, but I need this to construct an abstract syntax tree starting from a python formula at runtime, don't really want to alter its semantics in a weird way.

If not, I would like to modify the language itself to have this support. If there's some good expert that could suggest me the right way to do so please put your hands up, otherwise I think I'll ask Guido for that :)

Thanks a lot floks!


Solution

  • is, and, and or cannot be overloaded. Use the Python language services if you want to write a Pythonesque DSL.