Skip to content

Make the repr of lambda contain signature and body expression. #79037

Open

Description

@serhiy-storchaka
BPO 34856
Nosy @gvanrossum, @rhettinger, @terryjreedy, @serhiy-storchaka, @tirkarthi
PRs
  • gh-79037: Add signature and body expression to the repr of lambda #9647
  • 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 = None
    created_at = <Date 2018-09-30.18:14:23.113>
    labels = ['interpreter-core', 'type-feature', '3.8']
    title = 'Make the repr of lambda contain signature and body expression.'
    updated_at = <Date 2018-10-22.19:26:54.089>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2018-10-22.19:26:54.089>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2018-09-30.18:14:23.113>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34856
    keywords = ['patch']
    message_count = 8.0
    messages = ['326738', '326747', '327179', '327181', '327183', '327202', '327205', '328271']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'rhettinger', 'terry.reedy', 'serhiy.storchaka', 'xtreak']
    pr_nums = ['9647']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue34856'
    versions = ['Python 3.8']

    Activity

    1. serhiy-storchaka commented on Sep 30, 2018

      @serhiy-storchaka
      MemberAuthor
      >>> f = lambda x, y=1: x+y
      >>> f
      <lambda x, y=1: x + y at 0x7f437cd27890>

      Currently it is less informative: "<function <lambda> at 0x7f437cd27890>".

    2. gvanrossum commented on Sep 30, 2018

      @gvanrossum
      Member

      OTOH the current repr is bounded. Some people write very long lambdas.

      On Sun, Sep 30, 2018 at 11:31 AM Serhiy Storchaka <report@bugs.python.org>
      wrote:

      Change by Serhiy Storchaka <storchaka+cpython@gmail.com>:

      ----------
      keywords: +patch
      pull_requests: +9039
      stage: -> patch review


      Python tracker <report@bugs.python.org>
      <https://bugs.python.org/issue34856\>


      --
      --Guido (mobile)

    3. terryjreedy commented on Oct 5, 2018

      @terryjreedy
      Member

      Right. Like printing an ascii text version of the Mandelbrot set in one 20-line lambda + call expression statement. Let's truncate to, say, 40 chars. This should cover a large majority of lambda expressions.

    4. changed the title [-]Make the repr of lambda containing the signature and body expression.[/-] [+]Make the repr of lambda contain signature and body expression.[/+] on Oct 5, 2018
    5. serhiy-storchaka commented on Oct 5, 2018

      @serhiy-storchaka
      MemberAuthor

      People write also long strings, lists, dicts, but they are not truncated. In contrary to the above types which usually are created programmically and can be very large, lambdas are written manually and rarely exceed the size of a single line. We should discourage writing long lambdas. Local named functions are more appropriate for this. Although some people can use very long function names...

      Actually the repr of lambda can be very long now: <function VeryLongClassName.very_long_method_name.<locals>.<lambda> at 0x7fa2d9e04338>.

    6. gvanrossum commented on Oct 5, 2018

      @gvanrossum
      Member

      However, this is a compatibility liability. People routinely use various
      formatting options to truncate long strings, since experience shows those
      are common. But few people expect the repr() of a function/lambda object to
      be unwieldy.

    7. rhettinger commented on Oct 5, 2018

      @rhettinger
      Contributor

      +0 This would help with debugging and would compensate for the lack of a docstring. FWIW, I've found the new longer repr's for regex match objects to be helpful, and this would be another step in the right direction.

      Terry's suggestion to truncate a long repr makes sense. The repr's for long lists and dicts are different in that they are expected to round-trip, but there is no such expectation for lambdas.

    8. gvanrossum commented on Oct 6, 2018

      @gvanrossum
      Member

      OK, if it gets truncated beyond a reasonable length I remove my objection.

    9. terryjreedy commented on Oct 22, 2018

      @terryjreedy
      Member

      My understanding of the current code is that in the example above, f.__name__ would be "<lambda x, y=1: x + y>".

      1. I believe this would make the representation
        <<lambda x, y=1: x + y> at 0x........>
        unless functions gain a custom __repr__ method that strips the brackets off the name when present. I don't really like the alternative of leaving the brackets off the name.

      2. My proposal is to limit the length of the .__name__ attribute. This not only limits the repr but also the function name part of traceback lines. I consider the latter more important as I personally see function names in tracebacks far more often than in representations. Long function names wrapped to several lines would in my opinion negatively affect tracebacks. Large collections do not affect tracebacks.

    10. 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.8 (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