operators - Python: Concise way to toggle boolean "once" (i.e., in one direction) -
i find myself needing update boolean variable when happens first time (and then). using var = not var
out of question since continue flip-flopping every time.
(sorry silly example; i'm struggling find more sensible one…)
inner_has_been_two = false outer in range(5): inner in range(3): if inner == 2: if not inner_has_been_two: inner_has_been_two = true print(inner_has_been_two)
let's assume want "touch" variable little possible—otherwise overwrite again , again omitting innermost if
-statement.
basically i'm looking more terse, pythonic way emulate (the binary versions of ternary conditional operator, like) elvis operator (?:
) or null coalescing operator (e.g. ??
; varies upon language).
any ideas on how keep short(er) , clear?
setting true each time fast: single machine cycle, easy parallel process. if want logically trying do:
inner_has_been_true |= true
this single-cycle instruction, "bit set" operation. it's shorthand for
inner_has_been_true = inner_has_been_true | true
Comments
Post a Comment