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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'table.': 0.07; 'arguments': 0.09; 'arguments,': 0.09; 'compact': 0.09; 'variables.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'gui': 0.12; '"def"': 0.16; '24,': 0.16; 'direction?': 0.16; 'received:mac.com': 0.16; 'subject:simple': 0.16; "tim's": 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'received:10.0.1': 0.19; 'examples': 0.20; 'feb': 0.22; 'machine': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'this?': 0.23; 'cc:no real name:2**0': 0.24; 'sort': 0.25; 'pass': 0.26; 'code:': 0.26; 'certain': 0.27; 'values': 0.27; 'gets': 0.27; 'point': 0.28; 'function': 0.29; 'points': 0.29; 'url:mailman': 0.30; '8:30': 0.31; 'class': 0.32; 'extend': 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; 'charset :us-ascii': 0.36; 'possible': 0.36; 'received:10.0': 0.36; 'url:org': 0.36; 'received:10': 0.37; 'thank': 0.38; 'conditions.': 0.38; 'received:17': 0.38; 'pm,': 0.38; 'does': 0.39; 'bill': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'url:mail': 0.40; 'how': 0.40; 'new': 0.61; 'simple': 0.61; 'here:': 0.62; 'to:addr:gmail.com': 0.65; '2014,': 0.84; 'header:In-reply-to:1': 0.84; 'thing,': 0.91; 'state.': 0.95 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.11.87,1.0.14,0.0.0000 definitions=2014-02-24_03:2014-02-21,2014-02-24,1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1401130000 definitions=main-1402240179 Content-type: text/plain; charset=us-ascii MIME-version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: Coding a simple state machine in python From: William Ray Wing In-reply-to: <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com> Date: Mon, 24 Feb 2014 21:55:31 -0500 Content-transfer-encoding: quoted-printable References: <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com> To: Ronaldo X-Mailer: Apple Mail (2.1510) Cc: python-list@python.org, William Ray Wing 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393296954 news.xs4all.nl 2829 [2001:888:2000:d::a6]:39419 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67016 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: >=20 > if state =3D=3D "ABC": > do_something() > change state to DEF >=20 > if state =3D=3D "DEF" > perform_the_next_function() > ... >=20 > 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: >=20 > class TestClass(): > def __init__(self, var1, var2): #var1 and var2 are received from a = GUI > self.var1 =3D var1 > ... > if state =3D=3D "ABC" > doSomething(var1, var2) > .. >=20 > Could someone point me in the right direction? Thank you! >=20 > --=20 > 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: =20 = http://stackoverflow.com/questions/715457/how-do-you-implement-a-dispatch-= table-in-your-language-of-choice Bill=