Example:karfas wrote: ↑Wed Apr 27, 2022 8:41 pmI still don't get it. Maybe you can illustrate the problem with a few lines of code ?ardthusiast wrote: ↑Wed Apr 27, 2022 5:06 pmIt's for an event handler. Basically, when the event handler is called, I want it to invert the value of a boolean. The event handler can only execute functions, so I cannot use the not operator.
When the event handler wants a function, you will need to provide that function.
Even a (hypothetical) generic invert() call operating via a reference or pointer will need a parameter (that you most likely can't provide, as the interface to the event handler is a function!).
Code: Select all
from event_handler import EventHandler
bool1 = True
bool2 = False
event = EventHandler()
def invert(boolean_ref):
# boolean would be some reference to the boolean that I want to invert
boolean_ref = not boolean_ref
# Add 'invert(reference_to_bool1)' and 'invert(reference_to_bool2)' as subscribers to the event
event += [invert, [reference_to_bool1]]
event += [invert, [reference_to_bool2]]
# Event is called
event()
# Print the values of bool1 and bool2, if it works as intended the output should be 'False True'
print(bool1, bool2)
Code: Select all
from event_handler import EventHandler
booleans = {'bool1': True, 'bool2': False}
event = EventHandler()
def invert(boolean_ref):
# boolean_ref is the key of the booleans dictionary
booleans[boolean_ref] = not booleans[boolean_ref]
# Add 'invert(reference_to_bool1)' and 'invert(reference_to_bool2)' as subscribers to the event
event += [invert, ['bool1']]
event += [invert, ['bool2']]
# Event is called
event()
# Print the values of bool1 and bool2, if it works as intended the output should be 'False True'
print(booleans['bool1'], booleans['bool2'])