Let's Talk About Lambda Functions!

Daniel Fackrell unlearned at DELETETHIS.learn2think.org
Tue Jul 30 13:02:26 EDT 2002


"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.

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 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.

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.

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?

funcList = [ Insert idea here ]

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.

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





More information about the Python-list mailing list