Using the Python Interpreter as a Reference

Chris Angelico rosuav at gmail.com
Mon Nov 28 17:48:24 EST 2011


On Tue, Nov 29, 2011 at 9:24 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Because the syntax is completely different. One is a statement, and
> stands alone, the other is an expression. Even putting aside the fact
> that lambda's body is an expression, and a def's body is a block, def
> also requires a name. Using the same keyword for both would require
> special case reasoning: sometimes def is followed by a name, sometimes
> without a name. That's ugly.
>

All you need to do is define that a block of code is an object (and
thus suitable material for an expression), and you have easy
commonality.

fn = def(args):
    block
    of
    code

Now def is an expression that takes an optional name (omitted in the
above), an arg list, and a block of code... and there's minimal
difference between named and anonymous functions. (If it's given a
name, then it sets __name__ and also binds to that name, being
convenient for the common case. The above code is a silly way to do
almost the default.)

ChrisA



More information about the Python-list mailing list