On Tue, 23 Feb 2021 at 09:25, Steven D'Aprano <st...@pearwood.info> wrote: > > On Mon, Feb 15, 2021 at 09:29:45AM -0800, Guido van Rossum wrote: > > > Proposals like this always baffle me. Python already has both anonymous and > > named functions. They are spelled with 'lambda' and 'def', respectively. > > What good would it do us to create an alternate spelling for 'def'? > [...] > > > I think that the desire probably comes from thinking of short > mathematical functions where the body is a simple expression, like in > maths: [...] > So there is definitely some aesthetic advantage to the arrow if you're > used to maths notation, and if Python had it, I'd use it.
Yes, this is precisely my instinct as well. In addition, for whatever reason (and it's purely subjective, I won't try to defend it) I find "lambda" as a keyword somewhat alien in Python, where most other keywords are short terms or abbreviations of terms (and the terms are mostly non-technical English words - "with", "for", "def(ine)", "class", ...). So every time I use a lambda expression, it feels mildly unnatural to me, and I look for a "better way" of expressing my intent. But having a mathematical background, functional and comprehension styles feel natural to me, which introduces a tension - so, for example, I recently wrote some code that included the following: squares = set(takewhile(lambda sq: sq <= 2 * end, map(lambda x: x * x, count()))) A procedural version would be squares = set() x = 1 while True: sq = x * x if sq > 2 * end: break squares.add(sq) x += 1 and a *lot* of people would say this is easier to read, but IMO, both should have a comment explaining what "squares" is, and the itertools-based version took me less time to write, and ensure I'd got correct. I'd find a version using "arrow notation" squares = set(takewhile(sq => (sq <= 2 * end), map(x => x * x, count()))) easier to read than the lambda version (personally I prefer => over ->, but that's a detail) even though I needed to add parentheses to (sq <= 2 * end) to make it clear what was the expression and what was the argument. Honestly, none of these scream to me "the set of squares less than end * 2", though, so it's difficult to say any one of them is the "obvious" choice. But I'd probably order them as =>, lambda, procedural (from best to worst). This is when writing that code in a Jupyter notebook as part of some exploratory work. If this were production-level code where others would be maintaining it, I'd write a standalone function that wrapped the procedural code, give it a name and a docstring, and add some tests to it. But not all code is like that (not even all "important" code) so sometimes the one-liners make sense. > But it doesn't scale up to multi-statement functions, and doesn't bring > any new functionality into the language, so I'm not convinced that > its worth adding as a mere synonym for def or lambda or both. Yep, it's a difficult call. I suspect that if Python had been developed now, it would have followed the trend of using arrow notation. But adding it as a synonym for the existing lambda is nowhere near as obvious a choice. Paul _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/F4RPU5KZPNP3A6JWMORL4VC5TBTZJQMZ/ Code of Conduct: http://python.org/psf/codeofconduct/