Andrew Cooke | Contents | Latest | RSS | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Choochoo Training Diary

Last 100 entries

Surprise Paradox; [Books] Good Author List; [Computing] Efficient queries with grouping in Postgres; [Computing] Automatic Wake (Linux); [Computing] AWS CDK Aspects in Go; [Bike] Adidas Gravel Shoes; [Computing, Horror] Biological Chips; [Books] Weird Lit Recs; [Covid] Extended SIR Models; [Art] York-based Printmaker; [Physics] Quantum Transitions are not Instantaneous; [Computing] AI and Drum Machines; [Computing] Probabilities, Stopping Times, Martingales; bpftrace Intro Article; [Computing] Starlab Systems - Linux Laptops; [Computing] Extended Berkeley Packet Filter; [Green] Mainspring Linear Generator; Better Approach; Rummikub Solver; Chilean Poetry; Felicitations - Empowerment Grant; [Bike] Fixing Spyre Brakes (That Need Constant Adjustment); [Computing, Music] Raspberry Pi Media (Audio) Streamer; [Computing] Amazing Hack To Embed DSL In Python; [Bike] Ruta Del Condor (El Alfalfal); [Bike] Estimating Power On Climbs; [Computing] Applying Azure B2C Authentication To Function Apps; [Bike] Gearing On The Back Of An Envelope; [Computing] Okular and Postscript in OpenSuse; There's a fix!; [Computing] Fail2Ban on OpenSuse Leap 15.3 (NFTables); [Cycling, Computing] Power Calculation and Brakes; [Hardware, Computing] Amazing Pockit Computer; Bullying; How I Am - 3 Years Post Accident, 8+ Years With MS; [USA Politics] In America's Uncivil War Republicans Are The Aggressors; [Programming] Selenium and Python; Better Walking Data; [Bike] How Fast Before Walking More Efficient Than Cycling?; [COVID] Coronavirus And Cycling; [Programming] Docker on OpenSuse; Cadence v Speed; [Bike] Gearing For Real Cyclists; [Programming] React plotting - visx; [Programming] React Leaflet; AliExpress Independent Sellers; Applebaum - Twilight of Democracy; [Politics] Back + US Elections; [Programming,Exercise] Simple Timer Script; [News] 2019: The year revolt went global; [Politics] The world's most-surveilled cities; [Bike] Hope Freehub; [Restaurant] Mama Chau's (Chinese, Providencia); [Politics] Brexit Podcast; [Diary] Pneumonia; [Politics] Britain's Reichstag Fire moment; install cairo; [Programming] GCC Sanitizer Flags; [GPU, Programming] Per-Thread Program Counters; My Bike Accident - Looking Back One Year; [Python] Geographic heights are incredibly easy!; [Cooking] Cookie Recipe; Efficient, Simple, Directed Maximisation of Noisy Function; And for argparse; Bash Completion in Python; [Computing] Configuring Github Jekyll Locally; [Maths, Link] The Napkin Project; You can Masquerade in Firewalld; [Bike] Servicing Budget (Spring) Forks; [Crypto] CIA Internet Comms Failure; [Python] Cute Rate Limiting API; [Causality] Judea Pearl Lecture; [Security, Computing] Chinese Hardware Hack Of Supermicro Boards; SQLAlchemy Joined Table Inheritance and Delete Cascade; [Translation] The Club; [Computing] Super Potato Bruh; [Computing] Extending Jupyter; Further HRM Details; [Computing, Bike] Activities in ch2; [Books, Link] Modern Japanese Lit; What ended up there; [Link, Book] Logic Book; Update - Garmin Express / Connect; Garmin Forerunner 35 v 230; [Link, Politics, Internet] Government Trolls; [Link, Politics] Why identity politics benefits the right more than the left; SSH Forwarding; A Specification For Repeating Events; A Fight for the Soul of Science; [Science, Book, Link] Lost In Math; OpenSuse Leap 15 Network Fixes; Update; [Book] Galileo's Middle Finger; [Bike] Chinese Carbon Rims; [Bike] Servicing Shimano XT Front Hub HB-M8010; [Bike] Aliexpress Cycling Tops; [Computing] Change to ssh handling of multiple identities?; [Bike] Endura Hummvee Lite II; [Computing] Marble Based Logic; [Link, Politics] Sanity Check For Nuclear Launch; [Link, Science] Entropy and Life

© 2006-2017 Andrew Cooke (site) / post authors (content).

Python Enums on Crack, Part II

From: andrew cooke <andrew@...>

Date: Tue, 4 Jun 2013 08:50:28 -0400

Just under a month ago I wrote a disappointed rant[1] about the new Enum for
Python 3.

Since then I spent time extending the existing code[2] (to produce bnum[3]),
before changing direction and writing my own, alternative Enum from
scratch[4].  I've also swapped a few emails with various kind people.

While all that doesn't make me an expert, I do now have a better understanding
of the design and some of the choices it embodies.  So I thought I'd revisit
the points I made earlier and try explain why things work that way, where I
can, and what space for change still remains.


Implicit Values
---------------

In the original article I said that my first example -

  class Colour(Enum):
      red
      green

- was illegal syntax.  I was wrong.

When Python parses the class definition it looks for values in the dict of the
class that it is building.  Python 3's metaclass protocol allows us to provide
that dict, so we can use a subclass that assigns default values to unknown
names (and provide values from 'red' and 'green' instead of triggering an
error).

I use this trick in simple-enum[4], and the above is a valid declaration when
using that package.  But it's not a risk-free solution: there's the
possibility of very confusing errors / bugs.

The problem is that elsewhere in the code we may refer to a name in the global
scope.  The class dict will, incorrectly, provide a default value for that
name too.  And while you can reduce the effect[5], I am not (yet) convinced
that you can remove the risk entirely.


Implicit Values 2 (Strange Syntaxes)
------------------------------------

But all is not lost.  The PEP 0435 test code includes support for:

  class Colour(Enum):
      red = ...
      green = ...

which, although uglier, avoids the issues with "magic values".  This is not in
the default implementation, but it can be added by providing an alternate
metaclass).


Duplicate Values
----------------

Python 3's metaclass support includes keyword arguments.  Here's an example
from simple-enum:

  class WithAliases(Enum, implicit=False, allow_aliases=True):
      one = 1
      another_one = 1

Please ignore the 'implicit=False' (it does what you'd expect; it's needed
because of the shadowing issue I discussed earlier) - I am showing this
example because the 'allow_aliases' flag seems like a good solution to whether
or not duplicate values should be flagged: duplicates are errors by default,
but can be enabled if required.

Unfortunately I don't see anything likle this in the PEP 0345 code.  There's
no such flag (no metaclass keyword args are used at all) and no related tests.
But it is fairly easy to add - I included it in bnum[3].


Inheritance
-----------

The PEP Enum code is complicated by the need to support inheritance -
something that is used mainly (in the tests) to allow Enums to "be" integers.

In case the above is not clear, this is valid with PEP 0435:

  # PEP 0435
  class Number(int, Enum):
      one = 1
      two = 2

  > Number.one + Number.two
  3
  > isinstance(Number.one, Number)
  True
  > isinstance(Number.one, int)
  True

In contrast, the simple-enum implementation is, well, simpler, treating all
enumerations as named tuples (with name and value components), so equivalent
code would read:

  # simple-enum
  > Number.one.value + Number.two.value
  3

Now, after that long introduction, the question is: why does PEP 0435 have
this emphasis?  I chose the simple (but more verbose) solution because it
seemed easier to understand, simplified the implementation, and allowed me to
present enumerations in terms of both dicts and tuples (something I haven't
explained here, since I'm focussing on PEP 0435, but see [4] if you're
curious).

Nick Coghlan explained this to me, and it shone a completely new light on the
design: the PEP is designed to be backwards compatible with existing Python
library code.

For example, the socket module is full of constants like TIPC_ADDR_NAME and
AF_BLUETOOTH.  Existing code *expects* them to evaluate to whatever their
current value is.  Changing all that existing code to TIPC_ADDR_NAME.value is
impossible.  So the PEP Enum *must* "be" an int (or a str, or whatever the
original design chose) if these constants are going to be replaced by Enums.

Hence the inheritance.


Alternate Implcit Values
------------------------

I mentioned this in my rant, and support it in simple-enum with metaclass
keyword arguments (eg. 'values=from_zero'), but implicit values currently play
a very small part in the PEP 0435 Enum (they are available only through the
"functional API").


Language Changes
----------------

While the "names in a class" syntax is possible, it's something of a hack (see
above).  Since support for such a syntax might also help named tuples it's
interesting to ask whether the language could be extended in some way to
support this.  I don't have a concrete suggestion, but it seems like an
interesting avenue to explore.

A simpler change, which I stumbled across while considering alternatives, is
to extend matching to infinite sequences.  This would allow a syntax like:

  class SequenceBased(Enum):
      one, two, three, ... = count()


Summary
-------

Much of the design of the PEP 0435 Enum is driven by the requirement that it
be applied retroactively to existing classes (without changing existing code).
Honestly, to me, that seems a bit "too clever", but I understand the
temptation.

The "clean" syntax (names in a class) is possible in Python, but concerns
about opaque errors / bugs from name shadowing have excluded it from the PEP.
Alternative syntaxes (like 'name = ...') exist in the tests, but are not
enabled by default.

Duplicate values could be enabled by a named argument to the metaclass.  I
don't understand why this issue is missing from the code (even in the tests,
which otherwise do a good job of exploring alternative ideas).


[1] rant - http://www.acooke.org/cute/Pythonssad0.html
[2] PEP 0435 code - https://bitbucket.org/stoneleaf/ref435
[3] mod of [2] - https://github.com/andrewcooke/bnum
[4] new start - https://github.com/andrewcooke/simple-enum
[5] magic values - https://github.com/andrewcooke/simple-enum#the-danger-of-magic

The back-wards compatibility fallacy

From: Poul-Henning Kamp <phk@...>

Date: Tue, 04 Jun 2013 14:56:22 +0000

"Much of the design of the PEP 0435 Enum is driven by the
	requirement that it be applied retroactively to existing
	classes (without changing existing code).  Honestly, to me,
	that seems a bit "too clever", but I understand the
	temptation."

There is a finite amount of existing python code, it may be a lot of
python code, but it is a finite amount.

There is a unbounded amount of python code yet to be written and
in all likelyhood, much more python code will be written in the
future, than has been written in the past.

It is therefore a very short-sighted tradeoff and a grave mistake
to handicap such an important language tool as enums, just to cater
to the smaller of these two codebases.

If in doubt about this arguments validity, recall how
backwards-compat-at-all-cost-standardization has totally stopped
the evolution of the C language.

-- 
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk@...         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.

Comment on this post