On 11/3/20 3:58 PM, Steven D'Aprano wrote:
On Tue, Nov 03, 2020 at 11:10:37AM -0800, Ethan Furman wrote:

After discussions with Guido I made a (largely done) PR [3] which:

for stdlib global constants (such as RE)
    - repr() -> uses `module.member_name`
    - str() -> uses `member_name`

What's `RE`? Do you mean the enums in the re module, `re.I` etc?

Yes, re.RegexFlag.

for stdlib non-global constants, and enums in general
    - repr() -> uses `class.member_name`
    - str() -> uses `member_name`

How does that work? If I do this:

     class MyEnum(Enum):
         RED = 'red'

     RED = MyEnum.RED

is that a global constant or a non-global constant?

Up to you to decide.  ;-)

RegexFlag is a bit of a special case, so we'll look at socket instead.

Before Enum, socket had global constants such as

  AF_INET
  AF_UNIX
  SOCK_STREAM
  SOCK_DGRAM
  SOCK_RAW

then Enum came along and:

  IntEnum._convert_(
        'AddressFamily',
        __name__,
        lambda C: C.isupper() and C.startswith('AF_'))

  IntEnum._convert_(
        'SocketKind',
        __name__,
        lambda C: C.isupper() and C.startswith('SOCK_'))

which replaces the existing AF_* and SOCK_* constants with enums instead.

 How do I hook into whatever magic is used to decide one way or the other?

The only magic involved is choosing an appropriate `__str__` and `__repr__`. In 3.10, Enum._convert_ will choose a "global" `__str__` and `__repr__`.


The questions I would most appreciate an answer to at this point:

- do you think the change has merit?
- why /shouldn't/ we make the change?

It's not clear to me what the change actually is. You explained what the
behaviour is being changed to but not what it is being changed from.

My apologies.

For `socket` (where Enum is replacing preexisting global constants):

  str(socket.AF_INET)   # from  "AddressFamily.AF_INET"  to  "AF_INET"
  repr(socket.AF_INET)  # from  "<AddressFamily.AF_INET: 2>"  to  
"socket.AF_INET"


For `uuid`, which has a new Enum, SafeUUID (no preexisting global constants):

  str(SafeUUID.safe)    # from  "SafeUUID.safe"  to  "safe"
  repr(SafeUUID.unsafe) # from  "<SafeUUID.unsafe: -1>"  to  "SafeUUID.unsafe"


re.I has repr and str both return 're.IGNORECASE'. Will that change?

Yes -- the str would change to 'IGNORECASE'.


MyEnum.RED above has repr "<MyEnum.RED: 'red'>" and str 'MyEnum.RED'.
Presumably that will change to 'MyEnum.RED' and 'RED' respectively.

Assuming that regular Enum is changed (not just stdlib enums), then yes.

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

Reply via email to