[Python-ideas] Repr of lambda

Terry Reedy tjreedy at udel.edu
Sun Dec 17 15:55:16 EST 2017

On 12/17/2017 2:20 PM, Serhiy Storchaka wrote:
> Currently repr of doesn't contain much of information besides that it is 
> a lambda.
>  >>> lambda x: x**2
> <function <lambda> at 0x7f3479b74488>

> All lambdas have the same repr, different only by unreadable hexadecimal 
> address.

Having the same pseudo-name is what being anonymous means.  Some 
consider that a feature ;-).

> What if include the signature and the expression of the lambda in its repr?
>  >>> lambda x: x**2
> <lambda x: x**2>

Printing the return value requires adding a code or function attribute. 
The return expression(s), possibly None, would be just as useful for 
named functions.

> This would return an old feature of Python 0.9.1 
> (https://twitter.com/dabeaz/status/934835068043956224).
> Repr of function could contain just the signature.
> <function foo(bar, baz)>
> But there is a problem with default values. Their reprs can be very 
> long, especially in the following case with mutable default value:

I would not expect the representation to change; just use the expression.

> def foo(x, _cache={}):
>      try:
>          return _cache[x]
>      except KeyError:
>          pass
>      _cache[x] = y = expensive_calculation(x)
>      return y
> Maybe the placeholder should be always used instead of default values.

Do you mean 'a placeholder'?
Terry Jan Reedy

More information about the Python-ideas mailing list