Syntax Suggestion: Pass Function Definition as Argument

Chris Angelico rosuav at gmail.com
Fri Nov 8 07:00:36 EST 2019


On Fri, Nov 8, 2019 at 10:57 PM Antoon Pardon <antoon.pardon at vub.be> wrote:
>
> On 7/11/19 18:10, Stephen Waldron wrote:
> > What I'm aiming for is the ability to, within a function call, pass a suite that would be there automatically defined by the compiler/interpreter. Another comment did mention lambda functions, which does to some degree provide that capability, but is restricted to well, lambda functions (only expressions, not statements).
> I don't think those restrictions are that limiting. Certainly not since
> python3.8 and python acquired an assigment operator. And you should also
> note that a list is an expression. So we could do something like the
> following.
>

If you're implying that you can rewrite any function using lambda and
a list of expressions, then no, the ":=" operator won't help you:

>>> (lambda: x := 1)
  File "<stdin>", line 1
SyntaxError: cannot use named assignment with lambda

ChrisA


More information about the Python-list mailing list