4 ms·
Elliptical Python Programming
- benob 2y agoTIL that in python, 1--2==3
- seplox 2y agoIt's not a python thing. 1-(-2), distribute the negative.
- qsort 2y agoIn most C-like languages that would be a syntax error. E.g. in C and C++ as a rule you tokenize "greedily", "1--2" would be tokenized as "1", "unary decrement operator", "2", which is illegal because you're trying to decerment an rvalue. Python doesn't have "--", which allows the tokenizer to do something else.
- nyrikki 2y agoIn C, that is really because Unary minus (negation) has precedence over binary operations. +a - b; // equivalent to (+a) - b, NOT +(a - b) -c + d; // equivalent to (-c) + d, NOT -(c + d) https://en.cppreference.com/w/cpp/language/operator_arithmetic https://en.cppreference.com/w/cpp/language/operator_arithmet... +-e; // equivalent to +(-e), the unary + is a no-op if “e” is a built-in type // because any possible promotion is performed during negation already The same doesn't apply to, !! Which is applied as iterated binary operations (IIRC) I am pretty sure the decriment operator came around well after that quirk was established.
- seanhunter 2y agoPeter van der Linden’s book “Expert C Programming” (which is awesome btw) says that one of them (Kernighan, Richie or maybe Ken Thompson I forget) realised early on that the c compiler had the wrong operator precedence for bit twiddling and unary and boolean operators but “at that stage we had a few thousand lines of C code and thought it would be too disruptive to change it”
- j2kun 2y agoAlso worth noting that `1 - -2` works and produces 3 in C because the space breaks the operator.
- plus 2y agoFor those who are curious, `...` is a placeholder value in Python called Ellipsis. I don't believe it serves any real purpose other than being a placeholder. But it is an object and it implements `__eq__`, and is considered equal to itself. So `...==...` evaluates to `True`. When you prefix a `True` with `-`, it is interpreted as a prefix negation operator and implicitly converts the `True` to a `1`, so `-(...==...)` is equal to `-1`. Then, you add another prefix `-` to turn the `-1` back into `1`. `--(...==...)--(...==...)` evaluates to `2` because the first block evaluates to 1, as previously mentioned, and then the next `-` is interpreted as an infix subtraction operator. The second `-(...==...)` evaluates to `-1`, so you get `1 - -1` or `2`. When chaining multiple together, you can leave off the initial `--`, because booleans will be implicitly converted to integers if inserted into an arithmetic expression, e.g. `True - -1` -> `1 - -1` -> `2`. > There should be one-- and preferably only one --obvious way to do it. This article is obviously completely tongue-in-cheek, but I feel the need to point out that this sentence is not meant to be a complete inversion of the Perl philosophy of TIMTOWTDI. The word "obvious" is crucial here - there can be more than one way, but ideally only one of the ways is obvious.
- pletnes 2y agoNumpy actively uses … to make slicing multidimensional arrays less verbose. There are also uses in FastAPI along the lines of «go with the default».
- abuckenheimer 2y agoexcellent explanation, to add to this since I was curious about the composition, '%c' is an integer presentation type that tells python to format numbers as their corresponding unicode characters[1] so '%c' * (length_of_string_to_format) % (number, number, ..., length_of_string_to_format_numbers_later) is the expression being evaluated here after you collapse all of the 1s + math formatting each number in the tuple as a unicode char for each '%c' escape in the string corresponding to its place in the tuple. [1] https://docs.python.org/3/library/string.html#format-specification-mini-language https://docs.python.org/3/library/string.html#format-specifi...
- nomel 2y agoExpanding on this a little, I will be replacing all occurrences of 2 with two blobs fighting, with shields: >>> 0^((...==...)--++--(...==...))^0 2
- elijahbenizzy 2y agoOk do this but for JavaScript
- voidUpdate 2y agohttps://en.wikipedia.org/wiki/JSFuck https://en.wikipedia.org/wiki/JSFuck
- mariocesar 2y agoIf you're curious, the code in ellipsis results in executing: print('hello, world')
- mturmon 2y agoThank you! I noticed some ** and * in the thing sent to eval(), which (given that the building blocks are small integers) seemed related to prime factorizations. The initial %c is duplicated 21 times (3*7, if I read correctly), and then string-interpolated (%c%c%c...) against a long tuple of integers. These integers themselves are composed of products of factors combined using * and **. There is also one tuple "multiplication" embedded within that long tuple of integers -- (a,b)*2 = (a,b,a,b). That is for the 'l' 'l' in "hello". It's all very clever and amusingly mathy, with a winking allusion to the construction of natural numbers using sets. It made me Godel.
- callamdelaney 2y agoI think we're really starting to over crowd pythons syntax and I'm not a fan.
- MadVikingGod 2y agoThis behavior can be replicated with any class that has two special methods: __neg__ that returns -1 and __sub__ that accepts ints and returns 1-other. For example if you make this class: class _: def __neg__(self): return -1 def __sub__(self, other): return 1-other You get similar behavior: >>> --_() 1 >>> _()--_() 2 Fun python for everyone.
- maxloh 2y agoYou can do this on JavaScript too. alert(1) // equals to: [][(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[+!+[]+[+!+[]]]+[+!+[]]+([]+[]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[!+[]+!+[]]])() https://jsfuck.com/ https://jsfuck.com/
- deleted 2y ago[deleted]
- curtisszmania 2y ago[dead]
- budmichstelk 2y ago[dead]