[Python-ideas] Enums

Masklinn masklinn at masklinn.net
Mon Jul 25 21:25:39 CEST 2011


On 2011-07-25, at 21:09 , Michael Foord wrote:
> On 25 July 2011 19:47, Raymond Hettinger <raymond.hettinger at gmail.com>wrote:
>> On Jul 25, 2011, at 1:06 PM, Michael Foord wrote:
>> 
>> 
>> Python standard library modules currently using integers for constants:
>> 
>> * re - has flags (OR'able constants) defined in sre_constants, each flag
>> has two names (e.g. re.IGNORECASE and re.I)
>> 
>> 
>> What is being proposed here?  Will there be a new namespace so that we
>> would start writing re.Flags.IGNORECASE instead of re.IGNORECASE?  Are
>> module level constants now going to be considered bad-form?
>> 
>> If constants switch from re.IGNORECASE to re.flags.IGNORECASE, are there
>> any benefits to compensate for being both wordier and slower?
>> 
>> 
> Nope. Just something like this at the module level.
> 
> IGNORECASE = Flags.IGNORECASE
> 
> What it gains you is a nicer representation when looking at the values when
> debugging. On the other hand being able to have access to all the constants
> namespaced on a single object (as well) doesn't seem like such a bad thing.
Especially nice with unions/a good syntax for enumset. Because "74" being
re.IGNORECASE | re.MULITILINE | re.VERBOSE is not exactly obvious.

Whereas an str like

    <Enumset: {Flags.ignorecase, Flags.multiline, Flags.verbose} [int=74]>

could be rather nice (not overly fond of automatically OR-ing ints for
unions/sets, though, and thinking that there could be value in "enum values"
being singletons rather than values)


More information about the Python-ideas mailing list