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


Groups > comp.lang.python > #78901

Re: Toggle

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'familiarity': 0.07; 'blue': 0.09; 'toggle': 0.09; 'python': 0.11; 'def': 0.12; 'language.': 0.14; '(note': 0.16; 'enum': 0.16; 'finney': 0.16; 'properties,': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'library': 0.18; 'wed,': 0.18; '>>>': 0.22; 'print': 0.22; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; '>>>>': 0.31; 'writes:': 0.31; 'class': 0.32; 'implemented': 0.33; 'could': 0.34; 'received:google.com': 0.35; 'instances': 0.36; 'ben': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'between': 0.67
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=esuQLuAioNXFZJOdy/GdFMG4FC1GKz6IXdikPxLifaM=; b=mH7ZSXo0hdxqddsUpFH2SLbR1SNCcaPt/bnrq0nJPWcBuCCP3mHpaknYkXGE0vEAO1 Erjwz9nIkS3r6PV6TRz8bkRd6T+OV5mkGr7ajm0uvSVeD3foPbyJuq/IrCcsa2q4F0UY j/Hp0tFP3vN4cTNAgc7n1BPdHo6hTDiDkXrOZXEL/p8wIWj6rBEnqhmcRdTg6kNIJHlK +ULFfBeJzvLJJqtzD28mA5kgP0S/qi1u3DgDSyLZL1d+EOp5gnoYb8m9hPERkVf98F5M c2VdXxLOSqhvzff4tuQw+EHc7p45lRxTegOIFKkntXcL1sf9IV+pdJzDizVTiSx00uGk AeAw==
X-Received by 10.68.135.70 with SMTP id pq6mr10625424pbb.10.1412870888909; Thu, 09 Oct 2014 09:08:08 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <64c26ed7-33cc-4be9-93ac-8d2b84b92451@googlegroups.com>
References <u5kb3alsoleq8lh3eos289h5593m8033og@4ax.com> <mailman.14703.1412818949.18130.python-list@python.org> <64c26ed7-33cc-4be9-93ac-8d2b84b92451@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 9 Oct 2014 10:07:28 -0600
Subject Re: Toggle
To Python <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 <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.14721.1412870897.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1412870897 news.xs4all.nl 2880 [2001:888:2000:d::a6]:57430
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78901

Show key headers only | View raw


On Wed, Oct 8, 2014 at 8:34 PM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Thursday, October 9, 2014 7:12:41 AM UTC+5:30, Ben Finney wrote:
>> Seymore4Head writes:
>
>> > I want to toggle between color="Red" and color="Blue"
>
>> It's good to cultivate ongoing familiarity with the standard library
>
> And language. In recent python3:
>
>>>> class Color(Enum):
> ...   Red = 0
> ...   Blue = 1
> ...
>>>> Color.Red
> <Color.Red: 0>
>>>> print (Color.Red)
> Color.Red
>
> # Not sure what to make of that distinction...
>
>>>> c=Color.Red
>>>> c = Color.Blue if c==Color.Red else Color.Red
>>>> c
> <Color.Blue: 1>
>>>>
>
>>>> # Better
>>>> def toggle(c): return Color.Blue if c==Color.Red else Color.Red
> ...
>>>> toggle(c)
> <Color.Blue: 1>
>>>> toggle(c)
> <Color.Blue: 1>
>
> # which means the c has not changed

Python enums can have methods and properties, which means that toggle
could be implemented as such:

>>> class Color(Enum):
...   red = 0
...   blue = 1
...   def toggle(self):
...     return Color.blue if self is Color.red else Color.red
...
>>> Color.blue.toggle()
<Color.red: 0>
>>> Color.blue.toggle().toggle()
<Color.blue: 1>

(Note the recommended way to compare enum instances is with "is", not "==".)

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


Thread

Toggle Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-08 20:11 -0400
  Re: Toggle Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-09 12:29 +1100
    [OT] spelling colour / color was Re: Toggle mm0fmf <none@mailinator.com> - 2014-10-09 18:43 +0100
      Re: [OT] spelling colour / color was Re: Toggle duncan smith <buzzard@invalid.invalid> - 2014-10-09 19:24 +0100
        Re: [OT] spelling colour / color was Re: Toggle Tim Delaney <timothy.c.delaney@gmail.com> - 2014-10-10 06:53 +1100
          Re: [OT] spelling colour / color was Re: Toggle Marko Rauhamaa <marko@pacujo.net> - 2014-10-09 23:48 +0300
            Re: [OT] spelling colour / color was Re: Toggle alister <alister.nospam.ware@ntlworld.com> - 2014-10-10 17:31 +0000
              Re: [OT] spelling colour / color was Re: Toggle Marko Rauhamaa <marko@pacujo.net> - 2014-10-10 22:01 +0300
                Re: [OT] spelling colour / color was Re: Toggle alister <alister.nospam.ware@ntlworld.com> - 2014-10-10 20:05 +0000
                Re: [OT] spelling colour / color was Re: Toggle Christian Gollwitzer <auriocus@gmx.de> - 2014-10-11 11:37 +0200
                Re: [OT] spelling colour / color was Re: Toggle mm0fmf <none@mailinator.com> - 2014-10-11 12:45 +0100
                Re: [OT] spelling colour / color was Re: Toggle duncan smith <buzzard@invalid.invalid> - 2014-10-11 16:26 +0100
                Re: [OT] spelling colour / color was Re: Toggle Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-11 15:20 -0400
                Re: [OT] spelling colour / color was Re: Toggle William Ray Wing <wrw@mac.com> - 2014-10-11 15:55 -0400
                Re: [OT] spelling colour / color was Re: Toggle duncan smith <buzzard@invalid.invalid> - 2014-10-12 00:40 +0100
                Re: [OT] spelling colour / color was Re: Toggle giacomo boffi <pecore@pascolo.net> - 2014-10-18 09:06 +0200
                Re: [OT] spelling colour / color was Re: Toggle Simon Ward <simon+python@bleah.co.uk> - 2014-10-11 15:45 +0100
        Re: [OT] spelling colour / color was Re: Toggle "Neil D. Cerutti" <neilc@norwich.edu> - 2014-10-10 10:46 -0400
        Re: [OT] spelling colour / color was Re: Toggle Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-10 18:45 +0100
      Re: [OT] spelling colour / color was Re: Toggle Terry Reedy <tjreedy@udel.edu> - 2014-10-09 18:53 -0400
        Re: [OT] spelling colour / color was Re: Toggle Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-10 18:11 +1100
          Re: [OT] spelling colour / color was Re: Toggle Marko Rauhamaa <marko@pacujo.net> - 2014-10-10 13:08 +0300
            Re: [OT] spelling colour / color was Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-10 21:40 +1100
              Re: [OT] spelling colour / color was Re: Toggle Marko Rauhamaa <marko@pacujo.net> - 2014-10-10 13:51 +0300
                Re: [OT] spelling colour / color was Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-10 22:50 +1100
                Re: [OT] spelling colour / color was Re: Toggle Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-10 20:19 -0400
                Re: [OT] spelling colour / color was Re: Toggle Marko Rauhamaa <marko@pacujo.net> - 2014-10-11 13:33 +0300
                Re: [OT] spelling colour / color was Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-11 22:03 +1100
      Re: [OT] spelling colour / color was Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-10 10:21 +1100
      Re: [OT] spelling colour / color was Re: Toggle Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-10 17:52 +1100
        Re: [OT] spelling colour / color was Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-10 18:04 +1100
      Re: [OT] spelling colour / color was Re: Toggle Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-10 18:38 +0100
  Re: Toggle Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-09 02:25 +0100
  Re:Toggle Dave Angel <davea@davea.name> - 2014-10-08 21:24 -0400
  Re: Toggle Ben Finney <ben+python@benfinney.id.au> - 2014-10-09 12:42 +1100
    Re: Toggle Rustom Mody <rustompmody@gmail.com> - 2014-10-08 19:34 -0700
      Re: Toggle Steven D'Aprano <steve@pearwood.info> - 2014-10-09 04:56 +0000
        Re: Toggle Rustom Mody <rustompmody@gmail.com> - 2014-10-08 22:39 -0700
          Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-09 16:55 +1100
          Re: Toggle Peter Otten <__peter__@web.de> - 2014-10-09 09:51 +0200
            Re: Toggle Rustom Mody <rustompmody@gmail.com> - 2014-10-09 05:42 -0700
              Re: Toggle Terry Reedy <tjreedy@udel.edu> - 2014-10-09 16:57 -0400
          Re: Toggle Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-10 18:18 +1100
            Re: Toggle Rustom Mody <rustompmody@gmail.com> - 2014-10-10 07:24 -0700
              Re: Toggle Rustom Mody <rustompmody@gmail.com> - 2014-10-10 07:28 -0700
                Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-11 01:36 +1100
      Re: Toggle Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-09 10:07 -0600
        Re: Toggle Rustom Mody <rustompmody@gmail.com> - 2014-10-09 09:33 -0700
          Re: Toggle Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-10 18:15 +1100
  Re: Toggle Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-09 04:02 +0100
  Re: Toggle random832@fastmail.us - 2014-10-08 23:54 -0400
  Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-09 14:57 +1100
  Re: Toggle Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-09 17:57 +1300
    Re: Toggle Travis Griggs <travisgriggs@gmail.com> - 2014-10-08 22:38 -0700
    Re: Toggle alister <alister.nospam.ware@ntlworld.com> - 2014-10-09 15:23 +0000
      Re: Toggle Tony the Tiger <tony@tiger.invalid> - 2014-10-12 18:38 +0000
        Re: Toggle Chris Angelico <rosuav@gmail.com> - 2014-10-13 05:43 +1100
          Re: Toggle alister <alister.nospam.ware@ntlworld.com> - 2014-10-13 08:09 +0000
  Re: Toggle Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-10-10 13:44 +0200

csiph-web