python-peps/peps/pep-0312.rst

210 lines
5.9 KiB
ReStructuredText
Raw Normal View History

PEP: 312
Title: Simple Implicit Lambda
Version: $Revision$
Last-Modified: $Date$
2007-06-20 15:14:01 -04:00
Author: Roman Suzi <rnd@onego.ru>, Alex Martelli <aleaxit@gmail.com>
Status: Deferred
Type: Standards Track
2017-01-19 13:00:30 -05:00
Content-Type: text/x-rst
Created: 11-Feb-2003
Python-Version: 2.4
Post-History:
2003-02-14 09:51:27 -05:00
Abstract
2017-01-19 13:00:30 -05:00
========
This PEP proposes to make argumentless lambda keyword optional in
some cases where it is not grammatically ambiguous.
Deferral
2017-01-19 13:00:30 -05:00
========
The BDFL hates the unary colon syntax. This PEP needs to go back
to the drawing board and find a more Pythonic syntax (perhaps an
alternative unary operator). See python-dev discussion on
17 June 2005 [1]_.
2017-01-19 13:00:30 -05:00
Also, it is probably a good idea to eliminate the alternative
propositions which have no chance at all. The examples section
is good and highlights the readability improvements. It would
carry more weight with additional examples and with real-world
referents (instead of the abstracted dummy calls to ``:A`` and ``:B``).
2003-02-14 09:51:27 -05:00
Motivation
2017-01-19 13:00:30 -05:00
==========
2017-01-19 13:00:30 -05:00
Lambdas are useful for defining anonymous functions, e.g. for use
as callbacks or (pseudo)-lazy evaluation schemes. Often, lambdas
are not used when they would be appropriate, just because the
keyword "lambda" makes code look complex. Omitting lambda in some
special cases is possible, with small and backwards compatible
changes to the grammar, and provides a cheap cure against such
"lambdaphobia".
2003-02-14 09:51:27 -05:00
Rationale
2017-01-19 13:00:30 -05:00
=========
2017-01-19 13:00:30 -05:00
Sometimes people do not use lambdas because they fear to introduce
a term with a theory behind it. This proposal makes introducing
argumentless lambdas easier, by omitting the "lambda" keyword.
itself. Implementation can be done simply changing grammar so it
lets the "lambda" keyword be implied in a few well-known cases.
In particular, adding surrounding brackets lets you specify
nullary lambda anywhere.
2003-02-14 09:51:27 -05:00
Syntax
2017-01-19 13:00:30 -05:00
======
2017-01-19 13:00:30 -05:00
An argumentless "lambda" keyword can be omitted in the following
cases:
2017-01-19 13:00:30 -05:00
* immediately after "=" in named parameter assignment or default
value assignment;
2017-01-19 13:00:30 -05:00
* immediately after "(" in any expression;
2017-01-19 13:00:30 -05:00
* immediately after a "," in a function argument list;
2017-01-19 13:00:30 -05:00
* immediately after a ":" in a dictionary literal; (not
implemented)
2017-01-19 13:00:30 -05:00
* in an assignment statement; (not implemented)
2003-02-14 09:51:27 -05:00
Examples of Use
2017-01-19 13:00:30 -05:00
===============
2017-01-19 13:00:30 -05:00
1) Inline ``if``::
2017-01-19 13:00:30 -05:00
def ifelse(cond, true_part, false_part):
if cond:
return true_part()
else:
return false_part()
2017-01-19 13:00:30 -05:00
# old syntax:
print ifelse(a < b, lambda:A, lambda:B)
2017-01-19 13:00:30 -05:00
# new syntax:
print ifelse(a < b, :A, :B)
2017-01-19 13:00:30 -05:00
# parts A and B may require extensive processing, as in:
print ifelse(a < b, :ext_proc1(A), :ext_proc2(B))
2017-01-19 13:00:30 -05:00
2) Locking::
2017-01-19 13:00:30 -05:00
def with(alock, acallable):
alock.acquire()
try:
acallable()
finally:
alock.release()
2017-01-19 13:00:30 -05:00
with(mylock, :x(y(), 23, z(), 'foo'))
2003-02-14 09:51:27 -05:00
Implementation
2017-01-19 13:00:30 -05:00
==============
Implementation requires some tweaking of the ``Grammar/Grammar`` file
2017-01-19 13:00:30 -05:00
in the Python sources, and some adjustment of
``Modules/parsermodule.c`` to make syntactic and pragmatic changes.
2017-01-19 13:00:30 -05:00
(Some grammar/parser guru is needed to make a full
implementation.)
Here are the changes needed to ``Grammar`` to allow implicit lambda::
2017-01-19 13:00:30 -05:00
varargslist: (fpdef ['=' imptest] ',')* ('*' NAME [',' '**'
NAME] | '**' NAME) | fpdef ['=' imptest] (',' fpdef ['='
imptest])* [',']
2017-01-19 13:00:30 -05:00
imptest: test | implambdef
2017-01-19 13:00:30 -05:00
atom: '(' [imptestlist] ')' | '[' [listmaker] ']' |
'{' [dictmaker] '}' | '`' testlist1 '`' | NAME | NUMBER | STRING+
2017-01-19 13:00:30 -05:00
implambdef: ':' test
2003-02-14 09:51:27 -05:00
2017-01-19 13:00:30 -05:00
imptestlist: imptest (',' imptest)* [',']
2017-01-19 13:00:30 -05:00
argument: [test '='] imptest
2017-01-19 13:00:30 -05:00
Three new non-terminals are needed: ``imptest`` for the place where
implicit lambda may occur, ``implambdef`` for the implicit lambda
definition itself, ``imptestlist`` for a place where ``imptest``'s may
occur.
2003-02-14 09:51:27 -05:00
2017-01-19 13:00:30 -05:00
This implementation is not complete. First, because some files in
Parser module need to be updated. Second, some additional places
aren't implemented, see Syntax section above.
Discussion
2017-01-19 13:00:30 -05:00
==========
2017-01-19 13:00:30 -05:00
This feature is not a high-visibility one (the only novel part is
the absence of lambda). The feature is intended to make null-ary
lambdas more appealing syntactically, to provide lazy evaluation
of expressions in some simple cases. This proposal is not targeted
at more advanced cases (demanding arguments for the lambda).
2017-01-19 13:00:30 -05:00
There is an alternative proposition for implicit lambda: implicit
lambda with unused arguments. In this case the function defined by
such lambda can accept any parameters, i.e. be equivalent to:
``lambda *args: expr``. This form would be more powerful. Grep in the
standard library revealed that such lambdas are indeed in use.
2017-01-19 13:00:30 -05:00
One more extension can provide a way to have a list of parameters
passed to a function defined by implicit lambda. However, such
parameters need some special name to be accessed and are unlikely
to be included in the language. Possible local names for such
parameters are: ``_``, ``__args__``, ``__``. For example::
2017-01-19 13:00:30 -05:00
reduce(:_[0] + _[1], [1,2,3], 0)
reduce(:__[0] + __[1], [1,2,3], 0)
reduce(:__args__[0] + __args__[1], [1,2,3], 0)
2017-01-19 13:00:30 -05:00
These forms do not look very nice, and in the PEP author's opinion
do not justify the removal of the lambda keyword in such cases.
2003-02-14 09:51:27 -05:00
Credits
2017-01-19 13:00:30 -05:00
=======
The idea of dropping lambda was first coined by Paul Rubin at 08
Feb 2003 16:39:30 -0800 in comp.lang.python while discussing the
thread "For review: PEP 308 - If-then-else expression" [2]_.
References
==========
2017-01-24 10:23:46 -05:00
.. [1] Guido van Rossum, Recommend accepting PEP 312 -- Simple Implicit Lambda
2017-01-19 13:00:30 -05:00
https://mail.python.org/pipermail/python-dev/2005-June/054304.html
.. [2] Guido van Rossum, For review: PEP 308 - If-then-else expression
https://mail.python.org/pipermail/python-dev/2003-February/033178.html
2003-02-14 09:51:27 -05:00
Copyright
2017-01-19 13:00:30 -05:00
=========
This document has been placed in the public domain.
2003-02-14 09:51:27 -05:00
2017-01-19 13:00:30 -05:00
..
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End: