[Python-ideas] Does jargon make learning more difficult?

Abe Dillon abedillon at gmail.com
Tue Aug 21 18:02:07 EDT 2018


[Jonathan Fine]

> I have reservations about the name lambda. But there's a lot of
> code out there that uses lambda, and I'd like that code to continue to
> run.


Yes. I'm under no delusion that lambda will be replaced any time soon. Nor
do I believe any alternative I suggest will be enough of an improvement to
warrant breaking the "there should be one and only one obvious way to do
something rule". So I don't expect this conversation to lead to any change
to the language, I just think my case that it's sub-optimal might inform
future design decisions. Though it doesn't look like anyone finds it
especially convincing.

[Jonathan Fine]

> My understanding is that you prefer
> >>> EXPRESSION with IDEN
> to
> >>> lambda IDEN: EXPRESSION


Correct. My argument is two fold:

1) the name "lambda" is needlessly esoteric.
2) the format of lambdas is "backwards"

I tried to focus on the name gripe in this thread because trying to explain
both at the same time caused confusion. However, Stephen Turnbull's and
(more so) Steven D'Aprano's comments made the format argument semi-relevant
and I can't help getting on that soap-box...

[Jonathan Fine]

> How do you feel about this, as a means of defining an anonymous function?
> >>> with IDEN: EXPRESSION


I think it's better than lambda. I can't decide if I like it more than:

>>> def IDEN: EXPRESSION

Because def is more clearly connected to function declaration and i'm
somewhat sympathetic to the idea of expressionizing "with"-statements.
The only reason I don't prefer:

>>> EXPRESSION def IDEN

to

>>> EXPRESSION with IDEN

Is that it looks weird to me. It just doesn't seem right.

[Jonathan Fine]

> Now for a trick question. Consider
> >>> fn =  (lambda : EXPRESSION)
> This produces a function that has zero parameters.
> How would you write this, using 'with'. Would it be:
> >>> fn = (EXPRESSION with)
> I think this looks rather odd.


That's a good point. I hadn't considered that. There are several possible
formats that put the expression before the signature definition. The
specific form isn't quite as interesting to me. I would even prefer
something like:

>>> def (EXPRESSION [<seperator> SIG])

Mostly because "def" could then have a double meaning: "define" and
"deferred expression". I think that's kinda neat. My problem with putting
the signature first is that it usually amounts to noisy preamble that gets
in the way of the important code. In pseudo code you'd often write:

>>> hand = sorted(cards, by=card.suit)

But obviously the compiler doesn't know what "card" is since it's never
declared, so we write:

>>> hand = sorted(cards, by=lambda card: card.suit)

Notice that the information that's important to the reader: the expression,
is pushed behind information the computer needs but the reader probably
doesn't care about.
My claim is that in most cases, the signature is more noise than signal, so
it should come after the expression.

A function passed as a callback, key-function, mapper, reducer, filter,
event handler, etc. has a mostly pre-defined call signature. In most other
cases, you're better off using a named function.

Thanks for your input!

On Tue, Aug 21, 2018 at 3:51 PM, Jonathan Fine <jfine2358 at gmail.com> wrote:

> Hi Abe
>
> First, I have reservations about the name lambda. But there's a lot of
> code out there that uses lambda, and I'd like that code to continue to
> run.
>
> You wrote:
> > func = value[card.suit] if card not in wilds else wild_value with card
>
> I thought I'd try this, and variants, in the Python interpreter. I got
>
> # An expression. It's valid syntax, get run-time error.
> >>> value[card.suit] if card not in wilds else wild_value
> NameError: name 'card' is not defined
>
> # An expression. It's valid syntax. Its value is a function. No run-time
> error.
> >>> lambda card: value[card.suit] if card not in wilds else wild_value
> <function <lambda> at 0x7ff815e2bbf8>
>
> # If Python were enhanced, an valid expression whose value is a function.
> >>> value[card.suit] if card not in wilds else wild_value with card
> SyntaxError: invalid syntax
>
> My understanding is that you prefer
> >>> EXPRESSION with IDEN
> to
> >>> lambda IDEN: EXPRESSION
>
> How do you feel about this, as a means of defining an anonymous function?
> >>> with IDEN: EXPRESSION
>
> We can't adopt it, of course, because it's already valid syntax, but
> with semantics.
>
> >>> with wibble:
> ...    wobble
> NameError: name 'wibble' is not defined
>
> Now for a trick question. Consider
> >>> fn =  (lambda : EXPRESSION)
> This produces a function that has zero parameters.
>
> How would you write this, using 'with'. Would it be:
> >>> fn = (EXPRESSION with)
> I think this looks rather odd.
>
> --
> Jonathan
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180821/435b9e59/attachment-0001.html>


More information about the Python-ideas mailing list