This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Enum should have a __getattr__ that makes all the instances available from an instance
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman, lambacck, python-dev
Priority: normal Keywords: patch

Created on 2013-09-13 16:55 by lambacck, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
getattr_for_enum_class.patch lambacck, 2013-09-13 16:55 getattr for enum class patch review
issue19011.stoneleaf.01.patch ethan.furman, 2013-09-25 03:08 review
Messages (8)
msg197611 - (view) Author: Chris Lambacher (lambacck) * Date: 2013-09-13 16:55
The use case for this is that when you are in a template and you want to use the Enum instances in a conditional, then you need to pass the Enum class to the template or start using someenumvariable.__class__.someenumvalue. Instead it would be useful to be able to do someenumvariable.someenumvalue. Implimenting as __getattr__ on enum class allows real attributes to take precedence and also allows for overriding of behaviour in child classes.
msg197613 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-09-13 17:23
============================================================================
--> class Test:
...   this = 'that'
...   these = 'those'
... 
--> Test.this
'that'
--> Test.this.these
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'these'
============================================================================

This is not normal Python behavior, nor does it seem to be behavior intrinsic to enumerations.  It also adds an Alice-in-Wonderland quality to Enums:
============================================================================
--> red = Color.red
--> red.blue.blue.red.green.blue
<Color.blue: 3>
============================================================================

We're going to have to live without this particular "feature".
msg197616 - (view) Author: Chris Lambacher (lambacck) * Date: 2013-09-13 17:28
You are not comparing the same thing. Normally when there is a class parameter, those are available from instances of the class.

>>> class Test:
...    pass
...
>>> Test.this = Test()
>>> Test.that = Test()
>>> Test.this.that
<__main__.Test instance at 0x7ff681bd3560>
>>>
>>> isinstance(Test.this, Test)
True
>>> from enum import IntEnum

>>> class Color(IntEnum):
...     red = 1
...     blue = 2
...     green = 3
...
>>> isinstance(Color.red, Color)
True
>>>
msg197619 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-09-13 17:34
My apologies, you are correct.

I am still against this for the Alice reason, but lets see what the others think.
msg197620 - (view) Author: Chris Lambacher (lambacck) * Date: 2013-09-13 17:38
For what it's worth, I was confused by the inability to access the class members from the instance for like 3 or 4 weeks until I realized that the instances were not actually on the class and the implications of that for class attribute access.
msg198310 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-09-23 01:03
Posted a message on PyDev, but unless I get feedback saying it's a bad idea, or I find some implementation issue, I'll go ahead and make the change.

So either a doc patch or an enum patch will be in alpha3.  :)
msg198374 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-09-25 03:08
As discussed on PyDev[1], Enum members are bonafide class attributes, as in they show on the class, not on the instances.

Documentation patch attached.

[1] https://mail.python.org/pipermail/python-dev/2013-September/128874.html
msg198505 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-09-28 05:58
New changeset 47fb6b078b5f by Ethan Furman in branch 'default':
Close #19011:  Add documentation on some of the non-standard Enum behavior.
http://hg.python.org/cpython/rev/47fb6b078b5f
History
Date User Action Args
2022-04-11 14:57:50adminsetgithub: 63211
2013-09-28 05:58:23python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg198505

resolution: fixed
stage: patch review -> resolved
2013-09-25 03:08:01ethan.furmansetfiles: + issue19011.stoneleaf.01.patch
type: behavior
messages: + msg198374

stage: patch review
2013-09-23 01:03:06ethan.furmansetmessages: + msg198310
2013-09-13 17:38:59lambaccksetmessages: + msg197620
2013-09-13 17:34:34ethan.furmansetstatus: closed -> open
resolution: rejected -> (no value)
messages: + msg197619
2013-09-13 17:28:59lambaccksetmessages: + msg197616
2013-09-13 17:23:07ethan.furmansetstatus: open -> closed

nosy: + barry, eli.bendersky, ethan.furman
messages: + msg197613

assignee: ethan.furman
resolution: rejected
2013-09-13 16:55:43lambacckcreate