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


Groups > comp.lang.python > #106937 > unrolled thread

Re: Enum questions.

Started byMichael Selik <michael.selik@gmail.com>
First post2016-04-13 12:08 +0000
Last post2016-04-13 05:33 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Enum questions. Michael Selik <michael.selik@gmail.com> - 2016-04-13 12:08 +0000
    Re: Enum questions. Rustom Mody <rustompmody@gmail.com> - 2016-04-13 05:33 -0700

#106937 — Re: Enum questions.

FromMichael Selik <michael.selik@gmail.com>
Date2016-04-13 12:08 +0000
SubjectRe: Enum questions.
Message-ID<mailman.70.1460549338.15650.python-list@python.org>
On Wed, Apr 13, 2016, 12:14 PM Antoon Pardon <antoon.pardon@rece.vub.ac.be>
wrote:

> I have been looking at the enum documentation and it
> seems enums are missing two features I rather find
> important.
>
> 1) Given an Enum value, someway to get the next/previous
>    one
>
> 2) Given two Enum values, iterate over the values between
>    them.
>
> Did I miss those in the documentation or are they really
> missing?
>

An Enum corresponds to "nominal" data that is coded as a number simply for
storage rather than meaning. If you want next/previous you are thinking of
"ordinal" data which is coded as numbers for the purpose of comparison (but
not arithmetic). Placing nominal data in order would be comparing apples
and oranges, so to speak.

However, IntEnum should give you the features you want.

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

>

[toc] | [next] | [standalone]


#106938

FromRustom Mody <rustompmody@gmail.com>
Date2016-04-13 05:33 -0700
Message-ID<5abf839b-754b-47ed-9a5b-1d334838dd47@googlegroups.com>
In reply to#106937
On Wednesday, April 13, 2016 at 5:39:13 PM UTC+5:30, Michael Selik wrote:
> On Wed, Apr 13, 2016, 12:14 PM Antoon Pardon
> wrote:
> 
> > I have been looking at the enum documentation and it
> > seems enums are missing two features I rather find
> > important.
> >
> > 1) Given an Enum value, someway to get the next/previous
> >    one
> >
> > 2) Given two Enum values, iterate over the values between
> >    them.
> >
> > Did I miss those in the documentation or are they really
> > missing?
> >
> 
> An Enum corresponds to "nominal" data that is coded as a number simply for
> storage rather than meaning. If you want next/previous you are thinking of
> "ordinal" data which is coded as numbers for the purpose of comparison (but
> not arithmetic). Placing nominal data in order would be comparing apples
> and oranges, so to speak.
> 
> However, IntEnum should give you the features you want.
> 
> https://docs.python.org/3/library/enum.html#intenum
> 
> >

from enum import Enum
# General (not contiguous) enum
class Color(Enum):
    red = 10
    blue = 20
    green = 30


>>> Color.__members__
OrderedDict([('red', <Color.red: 1>), ('blue', <Color.blue: 2>), ('green', <Color.green: 3>)])

>>> set(Color.__members__)
set(['blue', 'green', 'red'])

>>> {n:Color[n] for n in set(Color.__members__)}
{'blue': <Color.blue: 20>, 'green': <Color.green: 30>, 'red': <Color.red: 10>}
>>> 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web