Re: [Python-ideas] Repr of lambda

2017-12-17 Thread Ivan Pozdeev via Python-ideas

On 17.12.2017 22:20, Serhiy Storchaka wrote:
Currently repr of doesn't contain much of information besides that it 
is a lambda.


>>> lambda x: x**2
 at 0x7f3479b74488>

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


What if include the signature and the expression of the lambda in its 
repr?


>>> lambda x: x**2



It's the same for named functions:

    In [1]: def ditto(a): return a

    In [2]: ditto
    Out[2]: 

Are you intending to do the same for them?
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.



But there is a problem with default values. Their reprs can be very 
long, especially in the following case with mutable default value:


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.

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


--
Regards,
Ivan

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Repr of lambda

2017-12-17 Thread Terry Reedy

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
 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



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.



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


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Repr of lambda

2017-12-17 Thread Serhiy Storchaka
Currently repr of doesn't contain much of information besides that it is 
a lambda.


>>> lambda x: x**2
 at 0x7f3479b74488>

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


What if include the signature and the expression of the lambda in its repr?

>>> lambda x: x**2


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.



But there is a problem with default values. Their reprs can be very 
long, especially in the following case with mutable default value:


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.

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/