What's TOTALLY COMPELLING about Ruby over Python?
adalke at mindspring.com
Mon Aug 18 21:06:34 CEST 2003
> What is lacking
> is a syntax to be able to create lambdas with multiple statements. And
> perhaps a syntax without using the keyword lambda, as some people seem
> to have an allergic reaction to it.
How about this?
def Lambda(x, y):
z = x+y
In more seriousness, Python makes a strong distinction betweeen statements
and expressions. Lambdas can be used in expressions, so if it includes
statements then what would would the layout look like which preserves
good Pythonic nature?
Here's one such example of defining a function for simple
def integrate(fctn, start = -1.0, end = 1.0, step = 0.1):
return sum(map(fctn, range(start, end, step)))/((end - start)/step)
print integrate(fctn = def (x):
print "Evaluating at", x,
if x > 0:
y = math.cos(x)
y = math.sin(x)
print "=", y
I think Python's parser can handle this. It has a certain charm
to it as well. But if the code is more than a line or two long then
I think it should be a named function. My main complaint is that
I can't stick a 'print' in the lambda, as for debugging. Occasionally
I'll have code akin to
print " ".join(map(str, arg)))
intergrate(lambda x: print_(x) and x)
which is a workaround. But I rarely need it, prefering named
functions over unnamed ones.
dalke at dalkescientific.com
More information about the Python-list