views:

33

answers:

1

Hello, 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!

+2  A: 

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

Ignacio Vazquez-Abrams
Thanks, you made the point. However I think I'll need to modify python directly to support this. It's a proof of concept for abstract interpretation techniques so I need to do this the way I said, cause I need to decide at runtime the path to be followed from existing code using these hooks.
hoheinzollern
You don't need to modify Python for this, viz. Ren'Py.
Ignacio Vazquez-Abrams
I don't get it, did they do this already in their engine?
hoheinzollern
They wrote an engine that lets you use scripts that are a mix of Python code and Ren'Py statements. What I'm getting at is that there's no need to modify it at the C level when all you need is a slightly more flexible way of parsing the code and generating the AST.
Ignacio Vazquez-Abrams