[Python-Dev] Re: str() vs format(): trivia question

2021年4月20日 12:51:01 -0700

On 4/20/21 12:01 PM, Guido van Rossum wrote:
> On Tue, Apr 20, 2021 at 11:12 AM Ethan Furman wrote:
>> Moving forward, I'm not sure having format() and str() ever be different
>> is a good idea, especially since users who need, for example, Color.RED
>> to be '1' can simply add a `__str__ = int.__str__` to their own custom
>> base IntEnum class and be good to go. If we deprecate the current behavior
>> now we could change it in 3.12.
>>
>> Thoughts?
> So to be clear, that one user wants f"{Color.RED}" to return "1" and not
> " Color.RED" (or something like that). And you want f"{Color.RED}" and
> str(Color.RED) to return the same value. Then together that means that
> str(Color.RED) must also return "1".
>
> Did I get that right? And are you happy with that outcome?
Almost right. They should both return `Color.RED`. Any users who want something different will need to do some work on their end:
 class MyIntEnum(IntEnum):
 def __format__ = int.__format__
 class Color(MyIntEnum):
 RED = 1
 format(Color.RED)
 # '1'
The deprecation period will give that user, and others like them, time to add their own Enum base classes with the `__format__` method they desire.
--
~Ethan~
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/GM2T4ATWPDAMYVELXRHJ4OJWNCD2T26Q/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to