Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'elif': 0.05; 'assignment': 0.07; 'problem:': 0.07; 'subject:query': 0.07; 'definition,': 0.09; 'modes': 0.09; '(note': 0.16; '0.1)': 0.16; '0.2': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'seconds,': 0.16; 'all.': 0.16; 'code.': 0.18; 'variable': 0.18; 'coding': 0.22; 'to:name :python-list@python.org': 0.22; "aren't": 0.24; 'subject:Code': 0.24; "haven't": 0.24; '(or': 0.24; 'long,': 0.26; 'nearly': 0.26; 'second': 0.26; 'least': 0.26; 'subject:/': 0.26; 'wondering': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'went': 0.31; 'code': 0.31; 'too.': 0.31; 'request,': 0.31; 'this.': 0.32; 'another': 0.32; 'says': 0.33; 'could': 0.34; "can't": 0.35; 'something': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'next': 0.36; 'application': 0.37; 'two': 0.37; 'easily': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'ago,': 0.61; 'reaching': 0.61; 'full': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'reach': 0.63; 'teaching': 0.64; 'comfort,': 0.84; 'layout.': 0.84; 'meters': 0.84; 'passenger': 0.84; 'instantly.': 0.91; 'power,': 0.91; 'imagine': 0.93; 'thereafter': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=gijvgrCFyNMCyYzbe/G+GzAYqKYM6q7F8dRxWApMv+s=; b=EUVJ+PbIBtYfOX07CQNMxKgxgvP89pr6emSROc/52mW7x1meTSH4CPK/XFp4KiOIf2 CV1wKkH8PlAts4z2+KY7AbNoI0VWT1gDrao8rhopgvvSh6xIGNFsbaARmfRQZrf1ik8U CwbSE8OX7RIOXlyPH72YIz9L9PVhwfkTuTtbrxqQX/EVgQT3Ka0IkjNNXxKyoLIYbQZL owhYCXQO6SAw2FuyVwTB82NqANxnOMA9JeZuvwCPHSAlOW9VdJmJmW9hxx8wuW/tXtbh mi3X5LM6ZvxeB5FvCEjHfb0gP66HZjnrWWRjavBmvPqYzL69kshm2HMGEH39SLIwqVIh 1CkA== MIME-Version: 1.0 X-Received: by 10.68.235.6 with SMTP id ui6mr25404818pbc.45.1396276389351; Mon, 31 Mar 2014 07:33:09 -0700 (PDT) Date: Tue, 1 Apr 2014 01:33:09 +1100 Subject: Code style query: multiple assignments in if/elif tree From: Chris Angelico To: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396276875 news.xs4all.nl 2949 [2001:888:2000:d::a6]:42143 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69432 Call this a code review request, if you like. I'm wondering how you'd go about coding something like this. Imagine you're in a train, and the brakes don't apply instantly. The definition, in the interests of passenger comfort, is that the first second of brake application has an acceleration of 0.2 m/s/s, the next second has 0.425 m/s/s, and thereafter full effect of 0.85 m/s/s. You have a state variable that says whether the brakes have just been applied, have already been applied for at least two seconds, or haven't yet been applied at all. Problem: Work out how far you'll go before the brakes reach full power, and how fast you'll be going at that point. Here's how I currently have the code. The variable names are a tad long, as this was also part of me teaching my brother Python. # Already got the brakes fully on if mode=="Brake2": distance_to_full_braking_power, speed_full_brake = 0.0, curspeed # The brakes went on one second ago, they're nearly full elif mode=="Brake1": distance_to_full_braking_power, speed_full_brake = curspeed - 0.2125, curspeed - 0.425 # Brakes aren't on. else: distance_to_full_braking_power, speed_full_brake = (curspeed - 0.1) + (curspeed - 0.4125), curspeed - 0.625 # If we hit the brakes now (or already have hit them), we'll go another d meters and be going at s m/s before reaching full braking power. But I don't like the layout. I could change it to a single assignment with expression-if, but that feels awkward too. How would you lay this out? (Note that the "else" case could have any of several modes in it, so I can't so easily use a dict.) ChrisA