Search code examples
pythonscripting-language

Proposal for python syntax - what would go into implemention?


I would like to use certain keywords and unary/binary operators as functions. For example:

_not = lambda a: not a
_plus = lambda a, b: a + b
_or = lambda a, b: a | b

I think there is an opportunity for a simpler syntax, which would look nicer in some functional settings:

_not = (not)
_plus = (+)
_or = (|)

The expressions on the right hand side are invalid anyway, so why not have them be those functions. I think it would also be nice to an identity lambda, maybe (:), as in (:) = lambda x: x.

So to make a question out of this: what would it take, broadly speaking, to implement these changes on my local installation of python and/or why is this a terrible idea?

Cheers


Solution

  • What you are trying to do here is to override the built in scope, enter image description here

    • It's a bad idea because, you are trying to override functionalities and syntax that all python/ Procedural/ object oriented programmers grew to use naturally.
    • and more importantly, as mentioned in the comments bellow the lines of code you mentioned are going to raise a syntaxError exceptions, meaning that somewhere in the binary 'python' program which interprets your python code, there exists rules and grammars that did not accept the kind of syntax which you wanted, meaning If you wanted a language which works that way, you would have to create it yourself (this would require you to understand what compilation means, the stages which a program goes through to become an executable, and maybe familiarity with libraries which facilitates this work for you) , or find an other languages that is similar to what you want.

    If you are really interested by following through with your question I think you should check functional programming langues like LISP, or try having a look at a python module called textX with which you can create your own language however you wanted to be like and then test if the idea is as good as you might think it is.