Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'exercise': 0.04; 'static': 0.04; 'value,': 0.04; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; '~ethan~': 0.09; 'def': 0.12; 'pressed': 0.16; 'received:69.93': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'skip:p 40': 0.19; 'later': 0.20; 'header:User- Agent:1': 0.23; "i've": 0.25; 'options': 0.25; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; 'class': 0.32; 'probably': 0.32; 'reader': 0.33; "can't": 0.35; 'there': 0.35; 'returning': 0.36; 'should': 0.36; 'wrong': 0.37; 'project': 0.37; 'button': 0.38; 'ends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'even': 0.60; 'entire': 0.61; 'received:173': 0.61; 'term': 0.63; 'become': 0.64; 'periodically': 0.68; 'default': 0.69; 'plc': 0.78; 'books.': 0.84 Date: Tue, 08 Apr 2014 17:22:12 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: "Latching" variables in function References: <7ffe44a68f47a64f5120d3a6c6a660b5@myglnc.com> In-Reply-To: <7ffe44a68f47a64f5120d3a6c6a660b5@myglnc.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator3304.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source-IP: 173.12.184.233 X-Exim-ID: 1WXgI2-0002kI-35 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:53810 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20= 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397004401 news.xs4all.nl 2843 [2001:888:2000:d::a6]:60496 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69915 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~