On Sat, Sep 19, 2020 at 1:29 AM Ethan Furman <ethan@stoneleaf.us> 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))
 >  >>> 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 top 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)

Looking over the stdlib enums, these usually already have their "kind" encoded in the name, e.g. AF_INET, SOCK_STREAM, SIGINT. And the re flags are pretty unique as well (IGNORE, MULTILINE, DOTALL).

--Guido van Rossum (python.org/~guido)