[Python-ideas] A simple proposal concerning lambda

Steven D'Aprano steve at pearwood.info
Wed Aug 22 13:22:33 EDT 2018


On Tue, Aug 21, 2018 at 09:57:51PM -0500, Abe Dillon wrote:
> [Chris Angelico]
> 
> > If you have a use-case for a lambda function that takes a callback and
> > has a default value for that callback, please submit it to The Daily
> > WTF. In Steve's example, the main function was created with a
> > statement.
> 
> 
> Fair enough, but I still think D'Aprano may have engineered his example to
> be particularly unreadable.

I picked the example to showcase an example of where this suggested new 
syntax looks bad. Part of debating proposals is to look at pros and 
cons, not just to look at the best cases.

But it wasn't an engineered example in the sense of "invented just for 
this purpose". Of course I used metasyntactic variables rather than real 
code, but I assure you I have real code that uses lambda in function 
parameter lists. Nor am I the only one.

For example, I have a private module (fortunately now retired) that 
backports itertools to older versions of Python, and it includes a 
version of groupby that looks like this:

class groupby(object):
    def __init__(self, iterable, key=lambda obj: obj):
        ...


I, er, "borrowed" a Prime number sieve class from somewhere (and forget 
where, so I can't give credit where credit is due) and it starts like 
this:

class Primes(object):
    def __init__(self, n=None, extend=False, exf=lambda x:x + x//2):


The std lib contains a test that this correctly raises SyntaxError:

    def f(*, x=lambda __debug__:0): pass

(the problem here is that __debug__ cannot be used as a parameter name).

So its not just me who uses lambda as a default argument.



> Why would you use 'arg' for both the argument to the method and the
> callback! That's at least a little WTF.

Heh, I didn't even notice that. It certainly wasn't intentional. Sorry.




-- 
Steve


More information about the Python-ideas mailing list