This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make default __format__ be equivalent to __str__
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-12-14 21:52 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
simpler-object-__format__.patch serhiy.storchaka, 2016-12-14 21:52 review
Pull Requests
URL Status Linked Edit
PR 506 merged serhiy.storchaka, 2017-03-06 09:53
Messages (4)
msg283220 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-14 21:52
Originally PEP 3101 defined the default __format__ implementation, object.__format__ as

     def __format__(self, format_spec):
         return format(str(self), format_spec)

After few changes (issue7994, issue28385) it now looks as

     def __format__(self, format_spec):
         assert format_spec == ''
         return format(str(self), '')

Proposed patch makes it yet simpler:

     def __format__(self, format_spec):
         assert format_spec == ''
         return str(self)

This is equivalent to the previous form except obscure case when str() returns not exact str, but strict subclass with overridden __format__.

The benefit of this change is simpler semantical model of the default implementation.

See the start of the discussion in issue28385 and the discussion on Python-Dev: https://mail.python.org/pipermail/python-dev/2016-October/146765.html.
msg293605 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-13 08:36
What are your thoughts about this Eric?
msg293607 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2017-05-13 09:08
I'm okay with this change.
msg293609 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-13 09:40
New changeset 7e19dbc92ec06a987eaae72f7cdfd32006aa4960 by Serhiy Storchaka in branch 'master':
bpo-28974: `object.__format__(x, '')` is now equivalent to `str(x)` (#506)
https://github.com/python/cpython/commit/7e19dbc92ec06a987eaae72f7cdfd32006aa4960
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73160
2017-05-13 09:41:23serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-05-13 09:40:54serhiy.storchakasetmessages: + msg293609
2017-05-13 09:08:03eric.smithsetmessages: + msg293607
2017-05-13 08:36:34serhiy.storchakasetmessages: + msg293605
2017-03-06 09:57:34rhettingersetnosy: + rhettinger
2017-03-06 09:53:17serhiy.storchakasetpull_requests: + pull_request415
2016-12-14 21:52:10serhiy.storchakacreate