r/programminghorror • u/nickthewildetype • Dec 08 '25
Does this qualify?

# Licensed under https://unlicense.org/
_flipFlopStateRegistry:dict[str,bool]=dict()
import inspect, time
def flipFlop(flip=True,flop=False):
try:returnVal=_flipFlopStateRegistry[flipFlopStateRegistry_key]=flip if flop==_flipFlopStateRegistry[flipFlopStateRegistry_key:=(stack:=inspect.stack()[1]).filename+str(stack.lineno)] else flop;return returnVal
except KeyError:_flipFlopStateRegistry[flipFlopStateRegistry_key]=flip;return flip
import random
def flipFlopRecursive():
print(flipFlop())
if random.random()>0.5:print(flipFlop("flip","flop"))
time.sleep(1)
flipFlopRecursive()
flipFlopRecursive()
22
Upvotes
View all comments
Show parent comment
1
u/nickthewildetype Dec 08 '25
As for example usecase... Blinking button would be the first thing that comes to mind? :-P
I were experiencing a bug where, sometimes the user input module I'm writing (keyboard/mouse input) would randomly stop working after registering and unregistering some WndProc function a couple of times. So I wanted to call 2 functions "activate" and "deactivate" endlessly in a loop for testing purposes and were like.: I can turn this into a single function rather than manually set a variable to True/False for every iteration!!! And so here we are using the inspect module just to toggle a simple boolean.