classification
Title: Make the repr of lambda contain signature and body expression.
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, rhettinger, serhiy.storchaka, terry.reedy, xtreak
Priority: normal Keywords: patch

Created on 2018-09-30 18:14 by serhiy.storchaka, last changed 2018-10-22 19:26 by terry.reedy.

Pull Requests
URL Status Linked Edit
PR 9647 open serhiy.storchaka, 2018-09-30 18:31
Messages (8)
msg326738 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-30 18:14
>>> 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>".
msg326747 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-09-30 21:04
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)
msg327179 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-10-05 19:38
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.
msg327181 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-05 19:57
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>.
msg327183 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-10-05 20:19
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.
msg327202 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-10-05 23:51
+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.
msg327205 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-10-06 00:30
OK, if it gets truncated beyond a reasonable length I remove my objection.
msg328271 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-10-22 19:26
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.
History
Date User Action Args
2018-10-22 19:26:54terry.reedysetmessages: + msg328271
2018-10-06 00:30:17gvanrossumsetmessages: + msg327205
2018-10-06 00:29:07xtreaksetnosy: + xtreak
2018-10-05 23:51:58rhettingersetnosy: + rhettinger
messages: + msg327202
2018-10-05 20:19:15gvanrossumsetmessages: + msg327183
2018-10-05 19:57:06serhiy.storchakasetmessages: + msg327181
2018-10-05 19:43:27terry.reedysettitle: Make the repr of lambda containing the signature and body expression. -> Make the repr of lambda contain signature and body expression.
2018-10-05 19:38:18terry.reedysetnosy: + terry.reedy
messages: + msg327179
2018-09-30 21:04:08gvanrossumsetmessages: + msg326747
2018-09-30 18:31:16serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request9039
2018-09-30 18:14:23serhiy.storchakacreate