Let's Talk About Lambda Functions!
Huaiyu Zhu
huaiyu at gauss.almadan.ibm.com
Tue Jul 30 18:37:53 EDT 2002
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
}
For vertical scoping, it is not necessary to separate items with commas.
>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
]
Huaiyu
More information about the Python-list
mailing list