[Python-Dev] Enum Eccentricities

Stephen J. Turnbull stephen at xemacs.org
Mon Sep 23 19:46:30 CEST 2013


Guido van Rossum writes:
 > On Mon, Sep 23, 2013 at 8:17 AM, Zero Piraeus <z at etiol.net> wrote:
 >> On Mon, Sep 23, 2013 at 09:45:46AM -0400, Chris Lambacher wrote:

 >>> [...] An example of how this will be used in practice is:>
 >>>     {% if object.state == object.state.completed %}
 >>>       some html
 >>>     {% endif %}

[Zero Piraeus suggests instead:]

 >>     {% if object.state == State.completed %}
 >>       some html
 >>     {% endif %}
 >>
 >> That's quite intelligible, and doesn't require anyone to know that
 >> an Enum member's siblings can, in your proposal, be accessed directly
 >> via dot notation (an unintuitive state of affairs, to me at least).

 > Right. The OP is just concerned that (because these are Django
 > templates) he will have to pass in the 'State' class as a separate
 > template parameter for this to work.

Given your earlier description of what makes sense for class
attributes, an alternative solution might be to put State-valued class
attributes (constants) on DjangoObject (the object's class), like
DjangoObject.completed_state = State.completed, and so on.  Then you
write "{% if object.state == object.completed_state %}".

IIUC, you wouldn't have a problem with that?  It still doesn't feel
quite right, but given the limitations of a template language, it
might grow on me.

Another alternative would be to have attributes like 'completed' be
*boolean* properties computed from a State-valued attribute, and write
just "{% if object.completed %}".  This actually feels good to me
given it's a templating language.

But I don't know if either of those is reasonable in the context.


More information about the Python-Dev mailing list