[Python-Dev] Enum Eccentricities

Guido van Rossum guido at python.org
Mon Sep 23 16:53:00 CEST 2013


On Mon, Sep 23, 2013 at 6:45 AM, Chris Lambacher <chris at kateandchris.net>wrote:

>
> On Sun, Sep 22, 2013 at 10:41 PM, Zero Piraeus <z at etiol.net> wrote:
>
>> I may be misunderstanding the use case given in the issue,
>>
>
> To clarify the use case, since it is my bug, this is so that the enum
> values are always available for comparison. The exact use case is in Django
> templates where a value comes from the database. If you want to compare you
> either have to use __class__ which I would say is a code smell, or you have
> to provide the Enum class. The code we are using for this is open source
> http://github.com/tindie/django-tidyenum. An example of how this will be
> used in practice is:
>
>     {% if object.state == object.state.completed %}
>       some html
>     {% endif %}
>

Now I see your use case. But I disagree that the best solution is to allow
accessing the enum values as attributes on object.state -- I would
recommend using __class__ to make it clear to the reader what's going on,
or to add the right Enum subclass to your template parameters. Tbe
expression you show in your example here will just baffle most readers who
haven't thought deeply about the issue.

(How would you compare a value that is a timedelta with a known timedelta?
You'd have to import the datetime module or use __class__, right?)


>  but it seems
>> to me that having to use
>>
>>     Color.red.__class__.blue
>>
>> (what is being complained about in the issue), while not exactly pretty,
>> makes a lot more semantic sense than
>>
>>     Color.red.blue
>>
>> ... which is just bizarre.
>>
>
> Any more bizarre than any other class that has properties of it's own type?
>

Yes, because you rarely if ever see them accessed that way.


>       a = 0
>       a.real.numerator.real.numerator
>

But that's different! Each of the attributes here (.real, .numerator) is
defined as an instance attribute.


>
> It is only weird because we are not used to seeing classes where the
> properties are instances of the class. I am not a core developer, but I
> have been programming in Python for more than 10 years, and I (and the
> other similarly experienced developers I work with) found that not having
> access to the class level properties was weird (yes I am aware that they
> are not actually class level properties but everywhere else Enum works hard
> to make it look like they are. See for instance the __dir__ method:
> http://hg.python.org/cpython/file/ed011b0d7daf/Lib/enum.py#l242).
>

Sorry to burst your bubble, but there is no rule that because you can
access something on the class you should be able to access it on the
instance. Try asking an instance for its class's __mro__ or __bases__.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130923/f6dc14c3/attachment.html>


More information about the Python-Dev mailing list