JavaScript's void operator in Python?

Jon Ribbens jon+usenet at unequivocal.eu
Sun Feb 2 18:29:47 EST 2020


On 2020-02-02, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> Jon Ribbens <jon+usenet at unequivocal.eu> writes:
>>Why does it matter if the return value is None?
>
>   In a lambda, a return value of None sometimes would be
>   convenient as by convention it indicates that the return
>   value does not carry any information and the function is
>   intended to be used just for its effect. A value of None
>   will not be printed in the console, so it does not add
>   distracting noise there.

If it's a lambda then the arguments will be evaluated before the
lambda is called anyway, so:

    >>> f = lambda *x: None
    >>> f(print(2), print(3))
    2
    3

Or if you're writing it inline then:

    >>> (None, print(2), print(3))[0]
    2
    3


More information about the Python-list mailing list