0

I saw some example python code:

for neighbor in active[i-(i>0):i+1]:

and just curious what i-(i>0) here do?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

1 Answers1

1

(i>0) is a condition that is True when i>0 and thus yields 1.

You can think of it also as active[i-{1 if i > 0, else 0}:i+1]:

A good comment by @Mad Physicist: bools are a subclass of int where {True,False} == {1,0}. You can find more on this here

Quote from the link above about this code:

No one would recommend using a boolean result in a numeric context

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124