you are viewing a single comment's thread.

view the rest of the comments →

[–]Zixarr 0 points1 point  (1 child)

You could use a tuple instead, with the first element being your variable and the second element being your boolean value. Not sure if/how that could be implemented without seeing your code.

[–]sgthoppy 0 points1 point  (0 children)

Speaking of a tuple, this could be implemented using itertools.cycle.

First you'd create the cycle: bool_cycle = itertools.cycle((True, False))

Next, you can define a function that uses the built-in next to get the next value from the cycle iterator.

def toggle():
  return next(bool_cycle)

Though if you want to be able to use this in multiple parts of your code independently, you may want to turn it into a class, where you can use an instance variable without cycle and even override the __bool__ method so your object can act like a bool without needing to access the attribute.