r/shittyprogramming • u/Wxyo • Aug 02 '22
Extensions to Python's operators +=, -=, etc.
In Python, you can rewrite
a = a + b
a = a - b
a = a * b
a = a / b
# etc. for //, &, |, ^, **
as
a += b
a -= b
a *= b
a /= b
# etc. for //=, &=, |=, ^=, **=
I propose to extend these based on the fact that I keep wanting to do stuff like this.
a = ~a --> a ~=
a = a[:-1] --> a [:=] -1
a = a[1:] --> a [=:] 1
a = f(a) --> a f=
a = f(a,b) --> a f= b
a, = a --> a ,=
a = [a] --> a [=]
a = a == b --> a === b
Guido plz
223
Upvotes
21
u/[deleted] Aug 03 '22
Once I wrote
node ->= leftinstead of
node = node->left13
u/jarfil Aug 03 '22 edited Dec 02 '23
CENSORED
9
u/[deleted] Aug 03 '22
Haskell (more correctly, Haskell libraries) has some fun operators. I've used
|*><*|in a project. There's also~=and.=and all sorts of other horrors.9
u/IIAOPSW Aug 03 '22
8===D
6
u/og_m4 Aug 03 '22
Gonna be such a pain to make a lexer for these
7
u/Wxyo Aug 03 '22
More general schema:
means
But unfortunately this isn't as pretty for some cases, e.g.
a f(=,b)rather thana f= b.3
u/[deleted] Aug 03 '22
Just looking at that “a ~=“ with no value being assigned is giving me nausea.
121
u/BetaDecay121 Aug 02 '22
Congratulations, you've reinvented the XNOR
97
u/veryusedrname Aug 02 '22
Thanks, this is horrible
85
u/TheZipCreator Aug 02 '22
new meaning to the triple equals, I like it
62
u/LaLiLuLeLo_0 Aug 02 '22
I propose an even better feature for implementing this, the
-->operator. It’s a template specifier, which specifies new syntactic sugar that translates the syntax on the right of the operator to the semantics on the left. Your suggestion already properly uses this!Allow me to demonstrate another fantastic example of its use:
So now we can finally add the one feature that is so sorely missing from Python, and do
18
u/justapassingguy Aug 02 '22
I'm pretty sure that this would cause to Python what Martin Luther caused to the Church
11
u/fungkadelic Aug 03 '22
i hate it
5
u/TigerEcstatic3894 Aug 03 '22
Thanks, I hate it :/
1
u/TheWittyScreenName Aug 03 '22
I’m pretty sure the first one works
3
u/[deleted] Aug 03 '22
fuck, i hate this so much... never have i seen such fuckery, and I have seen a lot.
Congrats on a high quiality shitpost there mate
1
u/great_site_not Aug 03 '22
2
u/Charlito33 Aug 03 '22
It's a better way to call functions /s
2
u/stdsort Oct 13 '22
I actually would like to be able to write
x min= a1
u/wrt-wtf- Jan 11 '23
You should be programming in Java…
1
u/Jinkweiq Aug 03 '22
It is possible to do this - im pretty sure numpy does something similar. You will have to write some C or use something like https://github.com/MatthieuDartiailh/bytecode - I’m pretty sure you can even add your own keywords
0
u/[deleted] Aug 13 '22
[deleted] — view deleted comment
3
u/[deleted] Aug 03 '22
[removed] — view removed comment