Anonymus functions revisited

Claudio Grondi claudio.grondi at freenet.de
Tue Mar 22 07:21:47 EST 2005


> > Why not:
> > try:
> >   (x,y,z)
> > except NameError:
> >   z=0
> >   (x,y,z)
> > ?
instead of
> > lambda x,y,z=0:(x,y,z)

> Because it does not do the same thing ?-)
Haven't got the idea why? (<pedantic?>)

> Because, if it did, it would still require 5 times more lines of code to
> do the same thing (hence it's more error-prone by a factor a 5) ?
I don't like to mix the idea of compression
with the idea of programming. The amount
of code doesn't matter much to me,
when readability suffers. This becomes
obvious, when one skips any comments and
name all variables with two letter codes
in the script, making it much more
compact.
By the way, I could also write:

try:(x,yz)
except NameError:z=0;(x,y,z)

reducing the number of lines to two,
but I don't, because in my eyes
I can read the code better if
spread over five lines and because
of this, the code becomes in my
opinion even less error-prone.
Actually I would write in my code:
try:
  (x,y,z)
except NameError:
  z=0
  (x,y,z)
#:try/except
inreasing the number of lines to
six in order to see directly, where
the end of try:/except construct is.

> Because it uses an exception, which is far less efficient than using a
> default argument ?
Efficiency is not the key point for me.
If I need efficiency I go to C(C++) anyway.
>From my past experience I know, that
defaults lead very often to problems,
because one tends to forget, that there
is a default value and is confused by
the outcome in which values not
explicitly mentioned in the
code occure, leading to deep
confusion (especially if hidden in
modules and libraries).
So I try to avoid defaults where it
is possible, using them if they don't
harm the outcome i.e. are related to
internal way of processing within
a function or are really obvious
and can be expected.

If the exception is the problem, why not
setting somewhere at the early beginning
of the code
z=0
and then use
(x,y,z)
?
Does the lambda in this case just not
try to get straight what was caused by
the bad concept and therefore bad
design of the script code flow?

> Because the name error could be on x and/or y too, and the try:except
> clause would hide it ?
I can't see why try:except hides it?
at least at the next line with
 (x,y,z)
the problem with x or y will cause
an error.

> > Watching the last piece of code
> > can even directly be seen, that there
> > is eventually a NameError
> > problem with z to handle,
> No. AFAICT, the name error could be with x and/or y and/or z.
see upper.

> > so where is the gain of using
> > lambda or the mapping?
Sorry, but I still can't see the advantage
of lambda, at least not in this example.

Claudio





More information about the Python-list mailing list