Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69946
| Date | 2014-04-09 08:34 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: "Latching" variables in function |
| References | <7ffe44a68f47a64f5120d3a6c6a660b5@myglnc.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9072.1397050472.18130.python-list@python.org> (permalink) |
On 2014-04-08 16:09, 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
>
> 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 what you want, you could do something like
class LatchButton:
def __init__(self, address):
self.value = 0
self.address = address
def __call__(self):
if not self.value:
if bus.read_byte_data(self.address, GPIOB) > 0:
self.value = 1
return self.value
button1 = LatchButton(address1)
button2 = LatchButton(address2)
for i in range(10):
print button1(), button2()
time.sleep(3)
-tkc
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: "Latching" variables in function Tim Chase <python.list@tim.thechases.com> - 2014-04-09 08:34 -0500
csiph-web