[Python-Dev] enum discussion: can someone please summarize open issues?
Steven D'Aprano
steve at pearwood.info
Mon Apr 29 03:52:51 CEST 2013
On 29/04/13 10:29, Ethan Furman wrote:
> On 04/28/2013 04:37 PM, Steven D'Aprano wrote:
>>> On Sun, Apr 28, 2013 at 12:32 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>>>
>>>> - should an enum item be selectable via __call__ instead of __getitem__
>>>> (i.e. Seasons(3) is AUTUMN)
>>
>> Does anyone know why this is even an issue? Is this pure bike-shedding over the API, or are there
>> technical reasons for choosing one over the other?
>
> This is an issue because currently every other type* in Python creates (or selects ;) its instances via the call syntax:
>
> - bool(1) # True
> - int('11') # 11
> - str(var) # whatever var had in it, now as a str
I think that's a red herring, because you're comparing the use of the object constructor with look-up by name.
Seasons[3] should not be considered as constructing an instance from argument 3, but a reverse lookup from raw value to enum value. Hence the use of __getitem__ rather than __call__.
> But one of the latest changes to flufl.enum was to take out the call syntax, and have only getitem syntax
>
> - Season('AUTUMN') # raises an exception
> - Season['AUTUMN'] # Season.AUTUMN
I'm not sure whether flufl.enums support creating additional instances after the event, but if it did, I would expect that I could say Season('WET') to get a new instance. I am indifferent to whether or not Season('AUTUMN') should return the existing AUTUMN enum value.
I think that I lean very slightly to these rules:
- Season(x) constructs new instances. If x is already a Season enum, it returns x; otherwise if x is a value already used for a Season enum, it raises.
- Season[x] looks up existing instances, and raises if x is not already a Season value.
but only very slightly.
> Not only is this inconsistent with the rest of Python*, but it's going to be a PITA for data storage/retrieval:
>
> datastore = dbf.Table('storage.dbf', 'event_name C(50); date D; season SEASON')
>
> def retrieve_record(...):
> result = []
> for field_type, field_data in record:
> result.append(field_type(field_data))
Instead of having field_type be Season, couldn't you make it Season.__getitem__ ?
--
Steven
More information about the Python-Dev
mailing list