Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69915 > unrolled thread

Re: "Latching" variables in function

Started byEthan Furman <ethan@stoneleaf.us>
First post2014-04-08 17:22 -0700
Last post2014-04-08 17:22 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: "Latching" variables in function Ethan Furman <ethan@stoneleaf.us> - 2014-04-08 17:22 -0700

#69915 — Re: "Latching" variables in function

FromEthan Furman <ethan@stoneleaf.us>
Date2014-04-08 17:22 -0700
SubjectRe: "Latching" variables in function
Message-ID<mailman.9048.1397004401.18130.python-list@python.org>
On 04/08/2014 01:09 PM, Grawburg wrote:
>
> I've probably used the wrong term - I'm thinking of what I do when writing PLC code - so I can't find how to do this in my reference books.
> This is part of a project I'm working on with a Raspberry Pi and an MCP23017 port expander.
> I have a N/O pushbutton that I want to "latch" a value to a variable when it's been pressed.  I have this function that gets called periodically in
> a 'while True' statement:
>
>
> def button():
>     pushbutton = 0
>    button_value = 0
>     pushbutton=bus.read_byte_data(address,GPIOB)
>     if pushbutton > 0:
>          button_value = 1
>     return button_value
>
>
>
>
> I need button_value to become '1' when the button is pressed and to remain '1' until the entire program (only about 25 lines) ends with a sys.exit()
>
>
> What do I use to 'latch' button_value?

If I understand correctly, once 'bus.read_byte_data()' has returned a non-zero value, 'button' should continue returning 
1 even if 'bus.read_byte_data()' later returns a 0?

There are a couple options for this:

   - use a default paramater as static storage

     def button(_latched=[0])
         push_button = _latched[0]
         if not push_button:
             button_value = bus.read_byte_data(address, GPIOB)
             if button_value > 0:
                 _latched[0] = push_button = 1
         return push_button

   - use a class

     # implementation left as an exercise for the reader  ;)

--
~Ethan~

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web