Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52078
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; '%s"': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '16)': 0.16; '@property': 0.16; 'enum': 0.16; 'formulation': 0.16; 'readable': 0.16; 'recipe': 0.16; 'subclassing': 0.16; 'logical': 0.24; '2.0': 0.26; 'code:': 0.26; 'raise': 0.29; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'anyone': 0.31; 'class': 0.32; 'quite': 0.32; 'minimal': 0.33; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; '3.4': 0.84; 'ordered.': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=kwPvCNHX9BGecxEBTn5NPZ1hwmas93KdztqsljMbao8=; b=JAKHYgVARSmysu06dcRU5TVNpSQGnNQSGdvlegqr2obwJxvaX7cHmX/DNGzBfIOJlg DeMjjt9yMwa7K3U0WX118QUR/IAUGrbGcANIHknkNjk+ieOxUmpGc+xB60CcmRwGgWy/ NT71Q2rUeMLssBW2vyA20ClMyFC2bRQxfHFzqIy5LudZwtNhKHy90dfacdE9YKGVYwVN kFCU+ttSU382WH3Ut4YOJiK4q86reX1k4BfPIhZUOyVmBwyigxgsAXe7SzIIMLUFwxoS /e+1TNctVcB8M0xvG8kHiq/Z00nJs2nQ28z0U0sgeWzLti6pMBbkgGc48RXbNP8Z9Igl /Clg== |
| X-Received | by 10.66.163.199 with SMTP id yk7mr1105181pab.136.1375830040369; Tue, 06 Aug 2013 16:00:40 -0700 (PDT) |
| MIME-Version | 1.0 |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Tue, 6 Aug 2013 17:00:00 -0600 |
| Subject | Enum vs OrderedEnum |
| 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 | <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.278.1375830043.1251.python-list@python.org> (permalink) |
| Lines | 37 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1375830043 news.xs4all.nl 15871 [2001:888:2000:d::a6]:39925 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:52078 |
Show key headers only | View raw
Using the OrderedEnum recipe from the Python 3.4 docs, I have the
following code:
class Environment(OrderedEnum):
gaia = 1
fertile = 2
terran, jungle, ocean, arid, steppe, desert, minimal = range(3, 10)
barren, tundra, dead, inferno, toxic, radiated = range(10, 16)
def is_standard(self):
return Environment.terran <= self <= Environment.minimal
def is_hostile(self):
return Environment.barren <= self
@property
def growth_factor(self):
if self.is_standard():
return 1.0
elif self.is_hostile():
return 0.5
elif self is Environment.fertile:
return 1.5
elif self is Environment.gaia:
return 2.0
else:
raise AttributeError("Unknown growth_factor for %s" % self)
This works, and the ordering is logical and intuitive, and I think
result is quite readable. That said, really the only reason for
subclassing OrderedEnum instead of Enum is to support the is_standard
and is_hostile methods. In normal usage there is no reason for the
members to be ordered. Can anyone suggest an alternative formulation
that is as readable as the above without relying on OrderedEnum?
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Enum vs OrderedEnum Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-06 17:00 -0600
csiph-web