David Bolen wrote:
So for example, an asynchronous sequence of operations might be like:

    d = some_deferred_function()
    d.addCallback(lambda x: next_function())
    d.addCallback(lambda blah: third_function(otherargs, blah))
    d.addCallback(lambda x: last_function())

which to me is more readable (in terms of seeing the sequence of
operations being performed in their proper order), then something like:

    def cb_next(x):
        return next_function()
    def cb_third(blah, otherargs):
        return third_function(otherargs, blah)
    def cb_last(x):
        return last_function()

d = some_deferred_function()
d.addCallback(cb_next)
d.addCallback(cb_third, otherargs)
d.addCallback(cb_next)
which has an extra layer of naming (the callback functions), and
requires more effort to follow the flow of what is really just a simple
sequence of three functions being called.

But this sequence contains an error of the same form as the "fat":

    while test() != False:
         ...code...

The right sequence using lambda is:
     d = some_deferred_function()
     d.addCallback(next_function)
     d.addCallback(lambda blah: third_function(otherargs, blah))
     d.addCallback(last_function)

And I would write it as:

     def third_function_fixed_blah(blah):
         def call_third(otherargs):
             return third_function(otherargs, blah)
         return call_third

     d = some_deferred_function()
     d.addCallback(next_function)
     d.addCallback(third_function_fixed_blah, otherargs)
     d.addCallback(last_function)

The name gives you the chance to point out that the argument order is
tweaked.  In many such cases, I use curry (ASPN recipe #52549), which
should show up in Python as "partial" in the "functional" module
according to PEP 309 <http://www.python.org/peps/pep-0309.html>
(accepted but not included).  I suppose it will show up in Python 2.5.

Programming is a quest is for clear, easy-to-read code, not quick,
easy-to-write code.  Choosing a name is a chance to explain what you
are doing.  lambda is used too often in lieu of deciding what to write.

--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to