fp/lambda question
Alex Martelli
aleax at aleax.it
Sat Apr 13 11:36:14 EDT 2002
Paul Rubin wrote:
> Alex Martelli <aleax at aleax.it> writes:
...
> the arcsin, etc). Using lambdas for the functions where there weren't
> ready-made built-ins seemed natural and obvious to me. Would you really
> have written separate definitions for those functions?
I might -- and I might organize the whole structure as one dictionary of
pairs (function, inversefunction) rather than two dictionaries of functions
(although I see where your organization might be handier, depending on how
the code is structured to use these tables). Neither assertion implies that
I consider your choices "wrong"! Just that we prefer different styles.
E.g.:
> unops = {'sqrt': math.sqrt,
...
> 'log': lambda x: math.log(x)/math.log(10),
...
> inv_unops = {'sqrt': lambda x: x*x,
...
> 'log': lambda x: 10.**x,
vs:
# unary functions that module math lacks
_1_over_ln_10 = 1.0 / math.log(10)
def log10(x): return math.log(x) * _1_over_ln_10
def pow10(x): return 10.0 ** x
def square(x): return x * x
# map name -> (function, inverse_function)
unops = { 'sqrt': (math.sqrt, square),
'log': (log10, pow10),
...
I _think_ this arrangement is fractionally clearer than the lambda-based
one. Just my opinion, of course.
Alex
More information about the Python-list
mailing list