Let's Talk About Lambda Functions!
John Roth
johnroth at ameritech.net
Wed Jul 31 09:24:47 EDT 2002
"Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote in message
news:slrnake5a1.467.huaiyu at gauss.almadan.ibm.com...
> Daniel Fackrell <unlearned at DELETETHIS.learn2think.org> wrote:
> >"John Roth" <johnroth at ameritech.net> wrote in message
> >news:ukddqtelmp6eda at news.supernews.com...
> >> 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.
> >
> >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.
>
> I don't quite get the idea why it has to indent 2. See examples
below.
>
> >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.
>
> Here's an example to exand on the idea.
>
> a = {
> 'func1': def (x,y):
> lots of things
> more things
> return x+y
> 'func2': def (x,y):
> lots of things
> more things
> return x-y
> 'func3': def (x,y):
> lots of things
> more things
> return x-y
> }
That works nicely, and you're right, you don't need two
dedents in this case, so you don't need two (virtual) indents either.
> For vertical scoping, it is not necessary to separate items with
commas.
Is this valid Python in general, or is this another change to the
syntax?
> >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 ]
>
> This does not look too bad, although it does take a while to get used
to.
> For simple functions still lambdas look better.
>
> curries = [
> def (x):
> return def (x,y):
> return x+y
>
> def (x):
> f = def (x,y):
> return x-y
> return f
>
> lambda x: lambda y: x*y,
>
> class:
> def __init__(self, x):
> self.x = x
> def __call__(self, y):
> return x/y
> ]
Another nice example. Thanks. Again, the commas
are missing, which may be a good thing, or it may
not.
John Roth
More information about the Python-list
mailing list