16

I "accidentally" came across this weird but valid syntax

i=3
print i+++i #outputs 6
print i+++++i #outputs 6
print i+-+i #outputs 0
print i+--+i #outputs 6 

(for every even no: of minus symbol, it outputs 6 else 0, why?)

Does this do anything useful?

Update (Don't take it the wrong way..I love python): One of Python's principle says There should be one-- and preferably only one --obvious way to do it. It seems there are infinite ways to do i+1

SysAdmin
  • 5,455
  • 8
  • 33
  • 34
  • 3
    You came across it? In an obfuscated coding contest? – extraneon May 21 '10 at 16:55
  • 1
    -1: What programming problem do you have to which this is the solution? Throw that code away, please. – S.Lott May 21 '10 at 16:56
  • 1
    You need to clean the crumbs out of your keyboard again. – Pete Kirkham May 21 '10 at 16:58
  • @Pete that orange juice is a lot harder to clean out... ;) – dash-tom-bang May 21 '10 at 17:01
  • 7
    @S.Lott - Am I asking for any solution here? Is it so wrong to ask why this is valid?. And btb i just accidentally typed i++1 in Python IDLE. Thats when I tried this. for a person from a c++ background this is weird. – SysAdmin May 21 '10 at 17:02
  • 1
    @SysAdmin: +1 absolutely a good question to ask, especially to understand the problems that syntax like ++i could lead to. – nico May 21 '10 at 17:06
  • 7
    +1, just because I can't understand why the heck anyone would -1 this. Must every question be ripped straight from some "practical" problem? – Derrick Turk May 21 '10 at 17:22
  • @Derrick Turk: Weird hypothetical questions with no practical purpose multiply without any sensible limitation. One could start typing even more random strings and post every single random string in this kind of question. The original `i++1` (only mentioned in a comment) is practical, focused and related to something sensible (i.e., c experience) The rest of the question is just accumulated cruft that **devalues** what could have been a sensible, simple focused question of value to others. – S.Lott May 21 '10 at 18:07
  • I, too, think it was an interesting question given my strong C background. Hence my hunting for the grammar rule that informs the behavior. Oftentimes unexplained behavior only becomes "weird" and "hypothetical" when the behavior is understood. – dash-tom-bang May 21 '10 at 18:07
  • 3
    @S. Lott: yeah, except that this "weird hypothetical" actually raises good questions about operator precedence, fixity, and composition. I've seen worse. – Derrick Turk May 21 '10 at 19:29
  • @Derrick Turk: Seeing worse doesn't make this better. Sticking to the real question without drifting into code obfuscation is an important part of clarity and focus. – S.Lott May 21 '10 at 20:19
  • 3
    @S.Lott congrats for encouring everyone to post questions. are you running for an S.O. Content-N*z* badge? seriously, i’ve been knowing that language for a decade now, and the question alongside with the examples and the answers were more amusing and teaching for me than some of the other comments, which i would not hesitate to label OT. just my 2c. – flow May 21 '10 at 22:03
  • 1
    There should one ... _obvious_ way to do it. Why would i ++++++ i be the 'obvious' way to add the two things? –  May 22 '10 at 08:17
  • possible duplicate of [Why does 1+++2 = 3 in python?](http://stackoverflow.com/questions/470139/why-does-12-3-in-python) – sth May 22 '10 at 13:50
  • Does this answer your question? [Why does 1+++2 = 3?](https://stackoverflow.com/questions/470139/why-does-12-3) – Georgy Jan 01 '21 at 16:50

3 Answers3

28

Since Python doesn't have C-style ++ or -- operators, one is left to assume that you're negating or positivating(?) the value on the left.

E.g. what would you expect i + +5 to be?

i=3
print i + +(+i) #outputs 6
print i + +(+(+(+i))) #outputs 6
print i + -(+i) #outputs 0
print i + -(-(+i)) #outputs 6 

Notably, from the Python Grammar Specification, you'll see the line:

factor: ('+'|'-'|'~') factor | power

Which means that a factor in an expression can be a factor preceded by +, -, or ~. I.e. it's recursive, so if 5 is a factor (which it is because factor->power->NUMBER), then -5 is a factor and so are --5 and --------5.

dash-tom-bang
  • 17,383
  • 5
  • 46
  • 62
5

The plus signs are considered unary operators to the right most i variable, as in +(-3) = -3, or +(+(+3))) = 3. Just the left most sign (plus or minus) are parsed as binary, so i+++i = i + (+(+i)), which translates to i + i = 3 + 3 = 6, in your example.

The other expressions follow the same principle.

Spidey
  • 2,508
  • 2
  • 28
  • 38
5

That should read

print i + (+ (+i) )

that is, the first sign is the addition operator, the other ones are infix signs

+i

and (unfortunately)

++i

are thus valid statements.

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77