Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making your own statements

Is there a way to define new statements like def, with, for of my own in Python? Of course, I don't mean to override the existing statements, only create some of my own.

If so, how do I do it? Can you point me to good docs on the subject?

like image 927
Guy Avatar asked Feb 08 '10 15:02

Guy


2 Answers

No, you cannot add new syntax within a Python program. The only way to alter the language is to edit and recompile the grammar file and supporting C code, to obtain a new altered interpreter, compiler and runtime.

like image 72
Alex Martelli Avatar answered Sep 28 '22 01:09

Alex Martelli


You can't (re)define language keywords without rewriting a compiler/interpreter/etc. What you could do perhaps is write a something like a DSL (domain-specific language) and something that translates your keyword statements into proper python statements, which might be an easier route.

like image 30
Davy8 Avatar answered Sep 27 '22 23:09

Davy8