I'm trying to have the return value of a function stored in a variable. The function is called (by a reference) when a Raspberry Pi gpiozero button is pressed.
from gpiozero import Button
from signal import pause
def fcn(a):
print(a)
b = a + 1
print(b)
return b
btn = Button(26)
i = 1
btn.when_activated = lambda: fcn(i) # returns 2
pause()
When the button is pressed it will print 1
and 2
as expected.
But how can I store the return value of fcn
into i
so it can increment at each button press?
Edit: The reason I've started with the when_activated
reference is that is in my main script I've got multiple buttons and multiple functions which can be pressed in any order and should pass around variables to each other. Something like this:
def fcn(a):
b = a + 1
return b
def fcn2(b):
c = b + 10
return c
btn1 = Button(26)
btn2 = Button(19)
i = 1
btn1.when_activated = lambda: fcn1(i) # returns i + 1
btn2.when_activated = lambda: fcn2(i) # returns i + 10
Actually I'm also passing around datetime
objects.