Proposal for python syntax - what would go into implemention?

82 views Asked by At

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

2

There are 2 answers

4
Pixel_teK On BEST ANSWER

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.

0
an inconspicuous semicolon On

In the python interpreters syntax analyser (how they work) certain keywords are considered reserved to the interpreter

for instance lets consider having a variable called def

this would cause a problem because how would python know if you are declaring a function of declaring a variable

to change this in your local install you would have to find the open source docs of python, find the part of the interpreting process that uses those keywords to something else to retain functionality e.g. change def to define so you could still declare functions

over all this would be way more hassle than its worth just to be able to have a var called def instead of _def