[Python-ideas] Should nested classes in an Enum be Enum members?

Serhiy Storchaka storchaka at gmail.com
Sat Jun 30 04:15:31 EDT 2018


29.06.18 03:25, Steven D'Aprano пише:
> On Thu, Jun 28, 2018 at 06:57:45AM +0300, Serhiy Storchaka wrote:
> 
>> Making a nested class a member you
>> don't lost anything, because you always can make it not-nested if you
>> don't want it be a member.
> 
> You lose the ability to have
> 
>      Colors.RED.NestedClass()  # returns something useful
>      # similar to Colors.RED.method()

Since NestedClass is an enum member, you should use 
Colors.RED.NestedClass.value() or just Colors.NestedClass.value() for 
calling its value.

If you don't want it be a member, just don't initialize it in the enum body.

class Color(Enum):
     RED = 1

class NestedClass:
     pass

Color.RED.NestedClass = NestedClass

Now Color.RED.NestedClass() can return something useful.



More information about the Python-ideas mailing list