"Latching" variables in function

Denis McMahon denismfmcmahon at gmail.com
Tue Apr 8 20:28:36 EDT 2014


On Tue, 08 Apr 2014 16:09:28 -0400, Grawburg wrote:

> def button():
>    pushbutton = 0
>   button_value = 0
>    pushbutton=bus.read_byte_data(address,GPIOB)
>    if pushbutton > 0:
>         button_value = 1
>    return button_value

Every time your function is called, you start out with button_value of 0.

You may need a global variable that starts out as False (or 0), and once 
flipped to True (or 1) and then stays there:

button_value = False # or: button_value = 0

def button():
    global button_value
    pushbutton = bus.read_byte_data( address, GPIOB )
    if pushbutton > 0:
         button_value = True # or: button_value = 1
    return button_value

Also I think I'd probably pass the IO address as a parameter to the 
button function.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list