This content originally appeared on DEV Community and was authored by tsuruko
Python library triggon
can dynamically switch values and functions, and it helps clean up your code by reducing the need for if
statements.
I'm going to introduce its features with two simple examples!
➀ Switch between values and functions
import random
from triggon import Triggon, TrigFunc
F = TrigFunc() # Wrapper for delayed execution
tg = Triggon("num", new=F.random.randint(1, 100))
def func():
# If 'num' is active, switch the value from 0 to random.randint(1, 100)
num = tg.switch_lit("num", org=0)
print(num)
func()
tg.set_trigger("num") # Activate the "num" label
func()
tg.revert("num") # Switch to the original value
func()
# == Output ==
0
79 (random number between 1 and 100)
0
switch_lit()
automatically executes the function if it is delayed with TrigFunc
.
TrigFunc
works by simply creating an instance and wrapping the target function, just like a normal function call.
➁ Switch to multiple index values for a single variable
F = TrigFunc()
# "nums" holds the values of (index 0, index 1, index 2, index 3)
tg = Triggon("nums", new=(1, 2, 3, F.print("Next it'll be 0!")))
x = 0
print(x)
tg.set_trigger("nums") # Activate "nums" label
tg.switch_var("nums", x) # Switch to index 0
print(x)
tg.switch_var("nums", x, index=1) # Switch to index 1
print(x)
tg.switch_var("nums", x, index=2) # Switch to index 2
print(x)
# If the value is a delayed function with TrigFunc,
# it will also execute automatically.
tg.switch_var("nums", x, index=3) # Switch to index 3
tg.revert("nums") # Switch to the original value of x
print(x)
# == Output ==
0
1
2
3
Next it'll be 0!
0
revert()
can restore the original value even if it's switched multiple times.
Those examples use one label but you can define any number of labels!
About triggon
triggon
has been officially released with 2 new functions and over 15 features added!
And it supports the following features:
- Dynamically switch values and functions
- Call functions and return early at any time
- Optionally return a value with an early return
- Delay the execution of almost any function
This library is for reducing if statements in your code!
If you’re interested, give it a try!
🔗 PyPI → https://pypi.org/project/triggon
🔗 GitHub → https://github.com/tsuruko12/triggon
I also post about the development and my learning logs on X.
Feel free to follow me!
🔗 X → @tool_tsuruko12
This content originally appeared on DEV Community and was authored by tsuruko

tsuruko | Sciencx (2025-08-28T13:01:21+00:00) Dynamically Switch Between Values and Functions — Without if Statements. Retrieved from https://www.scien.cx/2025/08/28/dynamically-switch-between-values-and-functions-without-if-statements/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.