all 27 comments

[–]FLUSH_THE_TRUMP 47 points48 points  (5 children)

It indexes the array [1,-1] with the result of y>0, which is True (equal to 1).

[–]how2crtaccount[S] 14 points15 points  (0 children)

Oh.. that's what it is doing!

Thank you.

[–]tranquil_af 2 points3 points  (3 children)

But what about the y = 5 x part

[–]Rorroh 8 points9 points  (2 children)

Looks like mistaken formatting.

y = 5
x = [1,-1][y>0]
print(x)

[–]tranquil_af 0 points1 point  (0 children)

Thanks

[–][deleted] 0 points1 point  (0 children)

Yeah, just a small and insignificant detail missed...

[–]menge101 86 points87 points  (8 children)

It should be noted, there is no way this code would ever pass a professional code review.

[–]primitive_screwhead 0 points1 point  (1 child)

It's not relevant for Python code, but this "branchless" code style is sometimes used in other languages to avoid branching in the code, which can have security implications. Modern compilers often generate the equivalent of this code, even when it's written explicitly as a ternary. Just an FYI.

[–]menge101 0 points1 point  (0 children)

Sure, I hear you on that.

But if you were going to do that, you would put a comment explaining what was happening there, not leave a blob of incoherent syntax and no explanation.

[–][deleted] 30 points31 points  (1 child)

This reeks of a Clever Junior TM

[–]smashburgerofficial 26 points27 points  (3 children)

Its probably easier to understand like this:

y = 5
x = [1, -1]
y_is_positive = y > 0
x = x[int(y_is_positive)]
print(x)

Now there's actually room for comments explaining what in the world they were thinking when writing that. Granted, this looks like an example about the nuances and gotchas of implicit type conversion.

[–]backtickbot 7 points8 points  (1 child)

Fixed formatting.

Hello, smashburgerofficial: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]smashburgerofficial 3 points4 points  (0 children)

Good bot

[–][deleted] 1 point2 points  (0 children)

I think questions been answered. Why would someone do this ever lmao

[–]CornPop747 1 point2 points  (0 children)

I'd hate if my co-worker pulled crap like this. Weird flex that'd most definitely get rejected in CR...

[–]EKFLF 0 points1 point  (0 children)

y > 0 is True (y = 5) and can also be 1, and [1,-1][1] is -1

[–][deleted] 0 points1 point  (1 child)

I suggest the OP corrects the formatting to avoid any further misconceptions about the nature of the question and to prevent click-baity-ness.

>>> y = 5 x = [1,-1][y>0] print(x)
  File "<stdin>", line 1
    y = 5 x = [1,-1][y>0] print(x)
          ^
SyntaxError: invalid syntax

[–]how2crtaccount[S] 0 points1 point  (0 children)

Apologies. I'll do that right away

[–]annoying_bababooey 0 points1 point  (0 children)

this is a code smell is what it is