First:

I started this specifically in the context of the stats package and the NaN handling flag, but it did turn into a ore general discussion of Enums, so a final thought:

On Tue, Aug 31, 2021 at 4:17 AM Ronald Oussoren <ronaldoussoren@mac.com> wrote:

Not just static typing, but static analysis in general. Tools like flake8 will complain about typos, completion tools can help typing, ….
... 
 
I tend to use constants instead of string literals because of better static analysis, and try to convert uses to enums over time.  String flags work as well, but I’ve had too much problems due to typo’s in string literals that weren’t caught by incomplete tests.  Being able to at least run a listing tool to find basic issues like typo’s is very convenient.  But YMMV.

I think this is the crux or it -- Enums are far more suited to static analysis.

And that reflects a shift in Python over the years. Most of the changes in Python have made it a better "systems" language, and some have made it a somewhat more awkward "scripting" language.

Features to support static analysis are a good example -- far less important for "scripting' that "systems programming".

Personally, I find it odd -- I've spent literally a couple decades telling people that Python's dynamic nature is a net plus, and the bugs that static typing (and static analysis I suppose) catch for you are generally shallow bugs. (for example: misspelling a string flag is a shallow bug).

But here we are.

Anyway, if you are writing a quick script to calculate a few statistics, I think a string flag is easier. If you are writing a big system with some statistical calculations built in, you will appreciate the additional safety of an Enum. It's hard to optimize for different use cases.

- CHB

--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython