On Tue, Apr 20, 2021 at 11:12 AM Ethan Furman <[email protected]> wrote:
> On 4/20/21 8:46 AM, Guido van Rossum wrote:
>
> > I'd guess it is totally up to the object, since str() calls `__str__`
> and format() calls `__format__`. Of course this
> > now begs the question whether those enums should perhaps change their
> `__format__` to match their `__str__`...?
>
> When Enum was designed we made sure and captured `__repr__` and `__str__`,
> but not `__format__`. So at this point,
> `__format__` is the mixed-in data type's -- so `int.__format__` in the
> case of IntEnum. However, if a user updates the
> `__str__` of their Enum, then that will be used in the format:
>
> ```python
> from enum import IntEnum
>
> class Color(IntEnum):
> RED = 1
>
> format(Color.RED)
> # '1'
>
> class Color(IntEnum):
> RED = 1
> def __str__(self):
> return 'one'
>
> format(Color.RED)
> # 'one'
> ```
>
> > But that
> > would not suit your purpose. Then again, how would one get the pretty
> IntEnum-specific representation in a format- or
> > f-string? I guess f"{flag!s}" would work.
>
> Yup, that does work.
>
> There is at least one user who is depending on `format()` using
> `int.__format__` because they filed a bug report when I
> broke it.
>
> 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?
--
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
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/DS6TLZJLMJJ5GP23ITNF74YPPFN7FU3Y/
Code of Conduct: http://python.org/psf/codeofconduct/