Anonymus functions revisited

Antoon Pardon apardon at forel.vub.ac.be
Tue Mar 22 08:09:17 EST 2005


Op 2005-03-22, Claudio Grondi schreef <claudio.grondi at freenet.de>:
> 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)
> ?

Because that wouldn't work with the kind
of code that was being discussed being:

 for x,y,z in some_iterator: 

If some_iterator produces at some time
a tuple with only two elements this
will raise an exception no matter
whether you assigned z already or not.


> 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?

Why should we assume the concept or design
was bad?  Suppose we have an algoritme
that works with 3d points. Now we find
out the algorithm works equally fine
with 2d points if we assume this 2d
points lie in the z plane. Wouldn't
giving z the value 0 in the case of
only two elements perfectly allow us
to use this 3d algorthm with 2d points?

Just because you can't imagine were this
would be helpfull doesn't make it a
bad concept or design.

>> > 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.

The lambda would allow us to insert the
functionallity locally without the need
to an function that is define somewhere
else.

-- 
Antoon Pardon



More information about the Python-list mailing list