0

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

Evan
  • 429
  • 4
  • 7

2 Answers2

0

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

0

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.

Pixel_teK
  • 793
  • 5
  • 16
  • 1
    This is completely wrong. If it were just a matter of changing the built-ins, it'd be pretty easy, and it wouldn't really involve overriding anything. The proposed syntax does not correspond to a built-in name lookup. The proposed syntax is not syntactically valid Python at all, and the difficulty lies in changing the grammar, parser, bytecode compiler, etc. to make it valid syntax. – user2357112 Oct 07 '20 at 22:36
  • 1
    The question isn't about how to make `_not` or `_plus` mean something. It's about how to make `(not)` or `(+)` mean something. – user2357112 Oct 07 '20 at 22:37
  • you mean to make them mean something else – an inconspicuous semicolon Oct 07 '20 at 22:39
  • I completely agree, code like this would work ```_not = lambda a: not a``` but as you mentioned overriding syntax at the lexical/grammatical level is just impossible, i completely agree. It s just that i didn t want to complicate my answer and make it intimidating. That s why i mentioned textX in which one has to completely define all of these rules. – Pixel_teK Oct 07 '20 at 22:41