Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Tue Jul 30 19:18:42 EDT 2002


"Daniel Fackrell" <unlearned at DELETETHIS.learn2think.org> wrote in
message news:3d46c6a2$1_1 at hpb10302.boi.hp.com...
> "John Roth" <johnroth at ameritech.net> wrote in message
> news:ukddqtelmp6eda at news.supernews.com...
> >
> > "Daniel Fackrell" <unlearned at DELETETHIS.learn2think.org> wrote in
> > message news:3d46964b$1_2 at hpb10302.boi.hp.com...
> > > "John Roth" <johnroth at ameritech.net> wrote in message
> > > news:ukb31ell2rh643 at news.supernews.com...
> > >
> > > <snip>
> > >
> > > >     for word in 'fee fie fo fum'.split():
> > > >         Button(frame, command = def ():
> > > >                 print word
> > > >             )
> > > >
> > > > This isn't really the world's best example, because IMO, the
> > > > lambda form is easier to read for something this small. However,
> > > > it does give the guts of the idea:
> > >
> > > <snip>
> > >
> > > > 2. The remainder of the syntax exactly models def, including
> > > > statement indentation within the expression. Notice that the
> > > > first statement in the anonamous function has to be indented
> > > > two from the preceeding line, because the continuation of
> > > > the expression has to still be indented from the line with the
> > > > 'def', and dedented from the statements.
> > >
> > > IIRC, there's no such ting as "indenting two" in Python as the
size of
> > an
> > > indentation is automatically detected as you go along, making the
> > following
> > > indentation style valid (although no one would ever want to do
it):
> > >
> > > toplevel
> > >  secondlevel
> > >                 thirdlevel
> > >  secondlevel
> > >     thirdlevel
> > >      fourthlevel
> > >  secondlevel
> > > toplevel
> >
> > The reason I say "indenting 2" is that you need two
> > dedents to make it work: one to close the def, which does
> > not have it's own indent, and one to finish off the expression
> > in which the def is imbedded.
> >
> > Does this make it clearer?
> >
> > John Roth
>
> Again, the problem is that Python must assume that when it sees code
> indented further than the previous line, a single indent is intended.
It
> has no way of knowing how many indents you mean unless something is
actually
> indented to each level along the way.

This is quite true, however what I meant to convey (and don't seem
to have done a very good job of it) is that since you need two dedents
to close the figure (one to end the def (or class) and one to end the
enclosing expression) for symetry you need two indents. The compiler,
in fact, wouldn't (and shouldn't) be able to tell the difference, but I
presume that
syntax aware editors might be able to. I said two indents not because
of compiler concerns, but because of syntax aware editor concerns.

> In addition to that, indentation is currently ignored inside (), {},
[],
> strings, and on lines following an escaped newline.  Some of that
could
> change, perhaps, but it seems to me that it might break existing code.

I don't see how it would break anything. The presence of 'def' or
'class'
would cause a shift into 'statement' mode. Those two reserved words are
not currently allowed within statements, only at the statement level.

> I do see merit in this discussion, though, because I see class and def
> statements as really being a shorthand for doing two things at once,
namely
> creating a function and then binding it to a name.

That's exactly the point.

> Admittedly, there are probably cases where the second part does not
apply
> all that well.  Perhaps a case where you want a list or dict of
functions
> would be one such case.  Binding to the name simply adds a step along
the
> way.  Passing a callable object as a parameter to another callable
object
> would probably be another such case.

Well, that's what's significant about anonymous functions. The inline
definition
results in the function object, which then has to be consumed by
something
that takes a function object and uses it. The statement version binds
the
function or class object to a name as part of it's execution.

> The issue quickly becomes readability, though.  Would you want to be
able to
> do a complex multi-line def or class statement inside an assignment
creating
> a list, for example?  And how would you do that without indentation?

I suppose you could. Again, if the list is supposed to contain
references to
function objects, I don't see why not.
>
> funcList = [ Insert idea here ]
# notice that the indentation isn't very obvious - problems with OE
funcList = [ def (a, b):
                    print a # indented 20 spaces
                    print b # indented 20 spaces
                ,   def (a, b): # indented 16 spaces
                    globalA = a # indented 20 spaces
                    globalB = b # indented 20 spaces
            ] # indented 12 spaces

Does this make sense?

>
> lambda in its current form does okay in cases like this.  As an
expression
> it does not require indentation for anything, but it is also not
allowed to
> execute statements.

Well, yes. And lambda generally does quite well for short functions.
The idea that inline functions should be short has a great number of
adherents.

John Roth
>
> --
> Daniel Fackrell (unlearned at learn2think.org)
> When we attempt the impossible, we can experience true growth.
>
>





More information about the Python-list mailing list