Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.035 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'cc:addr:python-list': 0.11; 'def': 0.12; '-tkc': 0.16; 'address):': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'pressed': 0.16; '\xc2\xa0if': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'class': 0.32; 'skip:b 30': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'something': 0.35; 'button': 0.38; '8bit%:4': 0.38; 'ends': 0.38; 'subject:" ': 0.39; 'entire': 0.61; 'address': 0.63; 'become': 0.64; 'received:50.22': 0.84; 'self.value': 0.84 Date: Wed, 9 Apr 2014 08:34:26 -0500 From: Tim Chase To: Grawburg Subject: Re: "Latching" variables in function In-Reply-To: <7ffe44a68f47a64f5120d3a6c6a660b5@myglnc.com> References: <7ffe44a68f47a64f5120d3a6c6a660b5@myglnc.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397050472 news.xs4all.nl 2879 [2001:888:2000:d::a6]:40581 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69946 On 2014-04-08 16:09, Grawburg wrote: > def button(): > =C2=A0 =C2=A0pushbutton =3D 0 > =C2=A0 button_value =3D 0 > =C2=A0 =C2=A0pushbutton=3Dbus.read_byte_data(address,GPIOB) > =C2=A0 =C2=A0if pushbutton > 0: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 button_value =3D 1 > =C2=A0 =C2=A0return button_value >=20 > 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() >=20 > 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 =3D 0 self.address =3D address def __call__(self): if not self.value: if bus.read_byte_data(self.address, GPIOB) > 0: self.value =3D 1 return self.value button1 =3D LatchButton(address1) button2 =3D LatchButton(address2) for i in range(10): print button1(), button2() time.sleep(3) -tkc