[Python-ideas] Ruby-style Blocks in Python Idea
Jim Jewett
jimjjewett at gmail.com
Wed Mar 11 03:44:43 CET 2009
On 3/10/09, Guido van Rossum <guido at python.org> wrote:
> On Tue, Mar 10, 2009 at 3:22 PM, Jim Jewett <jimjjewett at gmail.com> wrote:
>> On 3/10/09, Guido van Rossum <guido at python.org> wrote:
>>> On Tue, Mar 10, 2009 at 1:15 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>>>> 'Anonymous' is a different concept from 'expression-defined' despite
>>>> the tendency to conflate the two.
>>> ... Why is it important to you to split this particular hair?
>> An expression that *creates* and returns a function is useful.
>> A way to create unnamed functions may or may not be useful.
>> Right now, the two are tied together, as lambda is the best way to do
>> either. Mentally untangling them might lead to better code.
> I'm feeling really dense right now -- I still don't see the difference
> between the two. Are you saying that you would prefer an expression
> that creates a *named* function?
Yes.
The __name__ attribute might never be used, but I personally still
prefer that it be meaningful.
When someone says they need anonymous functions, I hear:
"I really, really *need* the __name__ to be useless!"
In the past, I had sometimes just assumed they were wrong.
Stephen has given me a glimpse of a mindset which really might need
the name to be useless. But neither he nor I normally think that way,
so it might still be YAGNI.
Terry has pointed out that they may actually mean "I need def to be an
expression", with the reference to anonymity being a red herring,
because the two concepts are currently confounded.
>> If the name in a def were optional, it would meet the perceived need
>> for anonymity, but still wouldn't meet the need for creating and
>> returning a function within a single expression.
> Moreover, unless you used a decorator, there would be no way to do
> anything with the anonymous function, so it would be useless.
Thus my troubles seeing the point of the people who care about
"anonymous functions."
>> On the other hand, if def became an expression, it would meet the need
>> for function-creating expressions (and would have at least reduced the
>> need for decorators).
> I don't see the conceptual difference between a "def-expression" (if
> it were syntactically possible) and a lambda-expression. What is the
> difference in your view?
The only differences *I* see are syntactical warts in lambda.
That said, I may be missing something myself, as lambda has had
passionate defenders.
>> add_callback(button1, def add3(a): return a+3)
> (1) Do you expect the name 'add3' to be bound in the surrounding scope?
No. But I agree that expectations would differ, so that either would
be acceptable, but either would feel like a wart to at least some
people.
> (2) What is the purpose of the name other than documenting the obvious?
It isn't always quite this obvious.
It is more useful in tracebacks. (Though perhaps printing source
instead of name would be even better, for functions short enough to be
reasonable expressions.)
The __name__ is available in case you want to use it for a dispatch
table, or to populate fields in an alternative User Interface. (For
example, accessibility APIs)
-jJ
More information about the Python-ideas
mailing list