Skip to content

Make default __format__ be equivalent to __str__ #73160

Description

@serhiy-storchaka
BPO 28974
Nosy @rhettinger, @ericvsmith, @serhiy-storchaka
PRs
  • bpo-28974: object.__format__(x, '') is now equivalent to str(x) #506
  • Files
  • simpler-object-format.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-05-13.09:41:23.437>
    created_at = <Date 2016-12-14.21:52:10.509>
    labels = ['interpreter-core', 'type-feature', '3.7']
    title = 'Make default __format__ be equivalent to __str__'
    updated_at = <Date 2017-05-13.09:41:23.436>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2017-05-13.09:41:23.436>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-05-13.09:41:23.437>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2016-12-14.21:52:10.509>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['45902']
    hgrepos = []
    issue_num = 28974
    keywords = ['patch']
    message_count = 4.0
    messages = ['283220', '293605', '293607', '293609']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'eric.smith', 'serhiy.storchaka']
    pr_nums = ['506']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue28974'
    versions = ['Python 3.7']

    Activity

    1. serhiy-storchaka commented on Dec 14, 2016

      @serhiy-storchaka
      MemberAuthor

      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 (bpo-7994, bpo-28385) 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 bpo-28385 and the discussion on Python-Dev: https://mail.python.org/pipermail/python-dev/2016-October/146765.html.

    2. serhiy-storchaka commented on May 13, 2017

      @serhiy-storchaka
      MemberAuthor

      What are your thoughts about this Eric?

    3. ericvsmith commented on May 13, 2017

      @ericvsmith
      Member

      I'm okay with this change.

    4. serhiy-storchaka commented on May 13, 2017

      @serhiy-storchaka
      MemberAuthor

      New changeset 7e19dbc by Serhiy Storchaka in branch 'master':
      bpo-28974: object.__format__(x, '') is now equivalent to str(x) (#506)
      7e19dbc

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions