What's TOTALLY COMPELLING about Ruby over Python?
Andrew Dalke
adalke at mindspring.com
Mon Aug 18 15:06:34 EDT 2003
Daniel Dittmar:
> 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
return z
print Lambda(2,3)
;)
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
numerical integration.
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)
else:
y = math.sin(x)
print "=", y
return 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
def print_(*args):
print " ".join(map(str, arg)))
return 1
intergrate(lambda x: print_(x) and x)
which is a workaround. But I rarely need it, prefering named
functions over unnamed ones.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list