map/filter/reduce/lambda opinions and background unscientific mini-survey
Steven Bethard
steven.bethard at gmail.com
Wed Jul 6 19:17:45 EDT 2005
Daniel Schüle wrote:
> Removing lamdba would be reduce readability of Python, I think here
> for examble of code like
>
> class App:
> ....
> ....
> def drawLines(self, event):
> from random import randint
> r = lambda : randint(1, 100)
> self.canvas.create_line(r(), r(), r(), r())
>
> defining one extra function would only confuse and
But you just did define one extra function!!
If you're really afraid of two lines, write it as:
def r(): randint(1, 100)
This is definitely a bad case for an anonymous function because it's not
anonymous! You give it a name, r.
> and what about creating one liner factories like
> from math import log10
> log = lambda basis: lambda x: log10(x) / log10(basis)
> log2 = log(2)
> log2(2**10) -> 10.0
This is slightly better, because at least one of your functions really
is anonymous. I'm not really sure I'm convinced that it's any better
than this though:
def log(base):
def log_in_base(x):
return log10(x)/log10(base)
return log_in_base
because when I first read the code, I didn't catch the second, nested
lambda. Of course, with a syntax-highlighting editor, I probably would
have.
Again though, to fix your abuse of anonymous function syntax for
non-anonymous functions, you should write this as:
def log(basis):
return lambda x: log10(x) / log10(basis)
Or if you're afraid of multiple lines:
def log(basis): return lambda x: log10(x) / log10(basis)
STeVe
More information about the Python-list
mailing list