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


Groups > comp.lang.python > #52095

Re: Enum vs OrderedEnum

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
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; 'attribute': 0.07; 'class,': 0.07; 'needed,': 0.07; '__init__': 0.09; 'arguments,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'imply': 0.09; 'message- id:@stoneleaf.us': 0.09; 'obj': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'def': 0.12; 'stored': 0.12; '*args):': 0.16; '@property': 0.16; 'all?': 0.16; 'attributes.': 0.16; 'enum': 0.16; 'readable': 0.16; 'received:70.85.130': 0.16; 'substitute': 0.16; 'such,': 0.16; 'wrote:': 0.18; 'pointed': 0.19; 'seems': 0.21; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'instead.': 0.24; 'integer': 0.24; 'fine': 0.24; 'question': 0.24; '2.0': 0.26; 'compare': 0.26; '----------': 0.26; 'values': 0.27; 'gets': 0.27; 'header:In- Reply-To:1': 0.27; "doesn't": 0.30; 'nature': 0.30; 'factor': 0.31; 'class': 0.32; 'guess': 0.33; 'minimal': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'there': 0.35; 'instances': 0.36; 'options:': 0.36; 'ordered': 0.36; 'turn': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'environment.': 0.39; 'itself': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'ian': 0.60; 'received:173': 0.61; 'more': 0.64; 'natural': 0.68; 'detail.': 0.68; '5:15': 0.84; 'arid': 0.84; 'desert': 0.84; 'enumeration': 0.84; 'ocean': 0.84; 'ordered.': 0.84; 'sets,': 0.84; '2013': 0.98
Date Tue, 06 Aug 2013 17:33:16 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: Enum vs OrderedEnum
References <CALwzidm5LSH891oAvK_tzNBY5K-BNS-D970Pa_LLxZXWCb6O9w@mail.gmail.com> <520182F1.2060709@stoneleaf.us> <CALwzidmhKKnDssRd97zRHhDjmwvDRrXVr+5TW4Zfs1iKWFKnOA@mail.gmail.com>
In-Reply-To <CALwzidmhKKnDssRd97zRHhDjmwvDRrXVr+5TW4Zfs1iKWFKnOA@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender ([173.12.184.233]) [173.12.184.233]:48297
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.291.1375835605.1251.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375835605 news.xs4all.nl 15944 [2001:888:2000:d::a6]:40558
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52095

Show key headers only | View raw


On 08/06/2013 04:46 PM, Ian Kelly wrote:
>
> On Aug 6, 2013 5:15 PM, "Ethan Furman" <ethan@stoneleaf.us <mailto:ethan@stoneleaf.us>> wrote:
>>
>> Use the .value attribute instead.  You could also substitute self for Environment.
>
> It feels more natural and readable to compare the enum instances rather than their value attributes. If I am ordering
> the values then that seems to imply that the enumeration itself is ordered. So I guess my question is better stated: is
> there a better way to do this that doesn't involve ordered comparisons at all?

If the only time you are using their Ordered nature is inside the class, it's an implementation detail.  As such, using 
the value seems fine to me.

There are other options:

   - using sets, as Rhodri pointed out (extra work is needed, though, because a set would want to turn into an Enum member)

   - using AutoNumber instead of Ordered, and specifing the growth factor directly


AutoNumber
----------

class AutoNumber(Enum):
     "ignore any arguments, __init__ can have them"
     def __new__(cls, *args):
         value = len(cls.__members__) + 1
         obj = object.__new__(cls)
         obj._value_ = value
         return obj

class Environment(AutoNumber):

     gaia = 2.0
     fertile = 1.5
     terran = 1.0
     jungle = 1.0
     ocean = 1.0
     arid = 1.0
     steppe = 1.0
     desert = 1.0
     minimal = 1.0
     barren = 0.5
     tundra = 0.5
     dead = 0.5
     inferno = 0.5
     toxic = 0.5
     radiated = 0.5

     def __init__(self, growth_factor):
         self._growth_factor = growth_factor

     @property
     def growth_factor(self):
         return self._growth_factor

This works because each Enum member gets its own integer value (1 - 15) in __new__, plus a growth factor that is stored 
by __init__.  Whether you think this is better I have no idea.  ;)

--
~Ethan~

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


Thread

Re: Enum vs OrderedEnum Ethan Furman <ethan@stoneleaf.us> - 2013-08-06 17:33 -0700

csiph-web