On 9/19/20 11:32 AM, Guido van Rossum wrote:
> On Sat, Sep 19, 2020 at 1:29 AM Ethan Furman wrote:
>> On 9/18/20 6:41 PM, Guido van Rossum wrote:

>>> Another brainstorm (or brainfart): maybe repr() should show the
>>> module/class and the name, and str() should only show the name. We'd
>>> then get
>>> ```
>>>  >>> # Mock-up!
>>>  >>> print(str(re.i))
>>> IGNORE
>>>  >>> print(repr(re.i))
>>> re.IGNORE
>>>  >>>
>>> ```
>>> and similar for Color.RED:
>>> ```
>>>  >>> # Another mock-up!
>>>  >>> print(str(Color.RED))
>>> RED
>>>  >>> print(repr(Color.RED))
>>> Color.RED
>>>  >>>
>>> ```
>>
>> I think that's too terse -- the first bit, whether class or module,
>> repr or str, is very important -- especially if you have several
>> enums using some of the same names for their members.
>
> Hm, the more I think about it the more I like this proposal. :-)
>
> When exploring in the REPL, my proposal *would* show the class name
> (but not the module name -- one can cause obfuscation that way too,
> but it would become unwieldy, and the custom seems to be to stop
> short of that).
>
> But when printing casually, wouldn't it be nice if we could cause
> end-user-friendly output to be produced by default? End users probably
> don't care about the class name, but they would care about the color
> name. E.g.
>
> class Color(Enum):
>      red = 0
>      green = 1
>      blue = 2
>
> class Flowers(Enum):
>      roses = 0
>      violets = 1
>
> def send_bouquet(flowers, color):
>      print("I'm sending you a bouquet of", color, flowers)

That's a fun example -- but member names are supposed to be UPPER_CASE, so your string would be, for example:

    I'm sending you a bouquet of RED VIOLETS

Of course, if we went with the idea of __str__ just returning the value, then:

  class Color(Enum):
      RED = 'red'
      GREEN = 'green'
      BLUE = 'blue'

  class Flowers(Enum):
      ROSES = 'roses'
      VIOLETS = violets'

which would indeed give us:

    I'm sending you a bouquet of red violets

and the above enums could be even simpler if using enum.auto() and a custom NamedEnum:

  class NamedEnum(Enum):
      """
      member values are the lower-cased member names
      """
      def _generate_next_value_(name, start, count, last_values):
          return name.lower()

  class Flowers(NamedEnum):
      ROSES = auto()
      VIOLETS = auto()

--
~Ethan~
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/QJX2D56FEEMQ5OFJPB5ASXAUM3GUWRPG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to