Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69475

Re: Code style query: multiple assignments in if/elif tree

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; 'syntax': 0.04; 'explicitly': 0.05; 'attribute': 0.07; 'subject:query': 0.07; '__init__': 0.09; 'aliases': 0.09; 'parameter': 0.09; 'python': 0.11; 'def': 0.12; '0.2': 0.16; '@property': 0.16; 'dict': 0.16; 'distinct': 0.16; 'enum': 0.16; 'likewise': 0.16; 'syntax,': 0.16; 'tempted': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'skip:p 40': 0.19; 'not,': 0.20; '>>>': 0.22; 'coding': 0.22; '31,': 0.24; 'mathematical': 0.24; 'planet': 0.24; 'satisfying': 0.24; 'subject:Code': 0.24; 'mon,': 0.24; "haven't": 0.24; 'looks': 0.24; '(or': 0.24; 'this:': 0.26; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'breaking': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'this.': 0.32; "we're": 0.32; 'quite': 0.32; 'url:python': 0.33; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'mass': 0.36; 'doing': 0.36; 'method': 0.36; 'url:org': 0.36; 'example,': 0.37; 'two': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'new': 0.61; 'url:3': 0.61; 'happen': 0.63; 'close': 0.67; 'mar': 0.68; 'approaches': 0.68; 'continuous': 0.68; 'duty': 0.68; '11:45': 0.84; '3.4': 0.84; 'me!': 0.84; 'self.value': 0.84; 'sorely': 0.84; 'state.': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=h+OakyRS20VoQMvxAOZypiyw8R7fOvQ/YVGcJhG4wa4=; b=cBTNLkCOqL0bmLUaB15HTPOU28rvb0UNEO2DLqMESLjgRKlhBYtGQvle09NWtDlXp7 tTW7soELPJXdRQ2qpZpD8pyZCBqyCYossypmjfUcIZkUYLOcwmggoQ925ULcVvM/HpO8 ejMg4XLAM4Sg3EaMYcz7Q8S8dO2CmdNl/soBMb86JHE7LJoFhrTEQjV7SAfkLr0J/Y5m eNUDLph0IhAF8S/kqGcMv8/cJWCDDJO8S5I9a6JWCFmGNXu7cS3I2L79T6TX9L67kIQu kzBNqkW9c2ckbljXrjMOEh0HHXOPR/XZVF26nTZ64yUnXtR0fVVQpOXU+R7hdfXZfGxz C2Dw==
X-Received by 10.66.141.144 with SMTP id ro16mr8141906pab.131.1396333773310; Mon, 31 Mar 2014 23:29:33 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <0f01ed1d-bc24-4286-8d1c-bfee7baedbc2@googlegroups.com>
References <mailman.8744.1396276875.18130.python-list@python.org> <533a3fd8$0$2909$c3e8da3$76491128@news.astraweb.com> <0f01ed1d-bc24-4286-8d1c-bfee7baedbc2@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Tue, 1 Apr 2014 00:28:53 -0600
Subject Re: Code style query: multiple assignments in if/elif tree
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8772.1396333782.18130.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396333782 news.xs4all.nl 2907 [2001:888:2000:d::a6]:42133
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69475

Show key headers only | View raw


On Mon, Mar 31, 2014 at 11:45 PM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Tue, Apr 1, 2014 at 3:26 PM, Steven D'Aprano  wrote:
>
>> Haskell has nifty pattern-matching syntax for this that looks quite close
>> to the mathematical hybrid function syntax, but in Python, we're limited
>> to explicitly using an if. If I were coding this, and I'm not, I'd wrap
>> it in a function. One advantage of a state variable rather than a
>> continuous time function is that we can do this:
>> def accel(state):
>>     return {NO_BRAKING: 0.0,
>>             LOW_BRAKING: 0.2,
>>             MID_BRAKING: 0.425,
>>             HIGH_BRAKING: 0.85}[state]
>
> Neat
> I would put the dict in a variable. And those _BRAKINGs are GALLing me!
>
> breaking = {NO:0.0, LOW:0.2, MID:0.425:, HIGH:0.85}
> def accel(state): return breaking[state]

If I were doing this in Python 3.4 I would be sorely tempted to use an
Enum for the state.  Then the acceleration could just be an attribute
(or method) of the state itself!

class BrakingState(Enum):
    no_braking = 0.0
    low_braking = 0.2
    mid_braking = 0.425
    high_braking = 0.85

    @property
    def acceleration(self):
        return self.value

>>> print(BrakingState.low_braking.acceleration)
0.2


Alternatively the __init__ method could set the acceleration directly
from the value(s) passed in, as in the Planet example:

https://docs.python.org/3/library/enum.html#planet

The problem I have with either of these approaches though is that if
two distinct states happen to have the same acceleration, they will be
considered aliases for the same state.  This is because the
acceleration value is doing double duty as a parameter of the state
and also as its identity.  Likewise in the Planet example, if a new
planet happened to have the same mass and radius as Jupiter, it would
be considered the same planet as Jupiter. I haven't managed to work
out a wholly satisfying way to avoid this.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 01:33 +1100
  Re: Code style query: multiple assignments in if/elif tree Marko Rauhamaa <marko@pacujo.net> - 2014-03-31 18:40 +0300
    Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 03:03 +1100
      Re: Code style query: multiple assignments in if/elif tree Rustom Mody <rustompmody@gmail.com> - 2014-03-31 09:20 -0700
        Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 03:29 +1100
          Re: Code style query: multiple assignments in if/elif tree "Rhodri James" <rhodri@wildebst.org.uk> - 2014-03-31 21:31 +0100
    Re: Code style query: multiple assignments in if/elif tree Ned Batchelder <ned@nedbatchelder.com> - 2014-03-31 17:42 -0400
    Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 09:50 +1100
    Re: Code style query: multiple assignments in if/elif tree Ben Finney <ben+python@benfinney.id.au> - 2014-04-01 09:57 +1100
    Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 10:12 +1100
      Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-01 00:57 +0000
    Re: Code style query: multiple assignments in if/elif tree Ethan Furman <ethan@stoneleaf.us> - 2014-03-31 17:30 -0700
  Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 04:26 +0000
    Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 16:01 +1100
      Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 07:20 +0000
        Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 18:35 +1100
          Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 08:07 +0000
            Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 19:12 +1100
        Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:18 -0600
        Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:24 -0600
    Re: Code style query: multiple assignments in if/elif tree Rustom Mody <rustompmody@gmail.com> - 2014-03-31 22:45 -0700
      Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 02:05 -0400
      Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 00:28 -0600
    Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 00:13 -0600
    Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 02:24 -0400
    Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 00:39 -0600
    Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 17:55 +1100
      Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 07:29 +0000
        Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 18:49 +1100
        Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:29 -0600
        Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 19:56 +1100
    Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:21 -0400
    Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 18:26 +1100
    Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:34 -0400
    Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:39 -0400
    Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:46 -0400
    Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:55 -0600

csiph-web