Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'table.': 0.07; 'arguments': 0.09; 'arguments,': 0.09; 'compact': 0.09; 'name?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'variables.': 0.09; 'python': 0.11; 'def': 0.12; 'gui': 0.12; '"def"': 0.16; '24,': 0.16; 'direction?': 0.16; 'function?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:simple': 0.16; "tim's": 0.16; 'wing': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'examples': 0.20; 'feb': 0.22; 'machine': 0.22; 'python?': 0.22; 'this?': 0.23; 'header :User-Agent:1': 0.23; 'sort': 0.25; 'pass': 0.26; 'code:': 0.26; 'certain': 0.27; 'values': 0.27; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'points': 0.29; 'url:mailman': 0.30; '8:30': 0.31; 'ray': 0.31; 'class': 0.32; 'extend': 0.32; 'another': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'really': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'next': 0.36; 'possible': 0.36; 'url:org': 0.36; 'thank': 0.38; 'conditions.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'url:mail': 0.40; 'how': 0.40; 'skip:t 30': 0.61; 'new': 0.61; 'simple': 0.61; 'here:': 0.62; 'william': 0.81; '2014,': 0.84; 'thing,': 0.91; 'state.': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Coding a simple state machine in python Date: Tue, 25 Feb 2014 11:04:26 +0100 Organization: None References: <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com> <41DAA444-F51A-458D-8F79-8CD3117A4879@mac.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdad84.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393322706 news.xs4all.nl 2874 [2001:888:2000:d::a6]:42465 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67037 William Ray Wing wrote: > > On Feb 24, 2014, at 8:30 PM, Ronaldo wrote: > >> How do I write a state machine in python? I have identified the states >> and the conditions. Is it possible to do simple a if-then-else sort of an >> algorithm? Below is some pseudo code: >> >> if state == "ABC": >> do_something() >> change state to DEF >> >> if state == "DEF" >> perform_the_next_function() >> ... >> >> I have a class to which certain values are passed from a GUI and the >> functions above have to make use of those variables. How do I go about >> doing this? I have the following algorithm: >> >> class TestClass(): >> def __init__(self, var1, var2): #var1 and var2 are received from a GUI >> self.var1 = var1 >> ... >> if state == "ABC" >> doSomething(var1, var2) >> .. >> >> Could someone point me in the right direction? Thank you! >> >> -- >> https://mail.python.org/mailman/listinfo/python-list > > And, to extend Tim's suggestion of a dictionary just a bit, note that > since Python functions are happy to pass function names as arguments, you > can use a dictionary to make a really nice compact dispatch table. That > is, function A does its thing, gets to a new state, and returns as one of > its return arguments the key into the dictionary that points to the next > function_name to be called based on that new state. > > Stackoverflow has a couple of compact examples here: > > http://stackoverflow.com/questions/715457/how-do-you-implement-a-dispatch- table-in-your-language-of-choice Why have the function return a name? Why not just another function?