Replace lambda with normals?

scott smarsh at hotmail.com
Thu May 3 19:41:27 EDT 2001


I'm going through the 'Dive into Python' tutorial and hit lambdas. I
know they are optional. As a beginner I would probably prefer they
weren't a language feature. Too many ways to do it. Anyway...
Question:
How would I replace the 2 lambdas in the following code from chapter 2
with normal functions?
----------------------------------------
def help(object, spacing=10, collapse=1):
    """Print methods and doc strings.

    Takes module, class, list, dictionary, or string."""
    methodList = [method for method in dir(object) if
callable(getattr(object, method))]
    processFunc = collapse and (lambda s: " ".join(s.split())) or
(lambda s: s)
    print "\n".join(["%s %s" %
                      (method.ljust(spacing),
                       processFunc(str(getattr(object,
method).__doc__)))
                     for method in methodList])
---------------------------------------

I tried creating separate functions and calling the functions e.g.:
#added new functions to the module e.g.
def shrinkIt(s):
    return " ".join(s.split())
#tried to call it
processFunc = collapse and shrinkIt() or leaveIt()

Of course it didn't work. How do I do it right?
Thanks in advance.












More information about the Python-list mailing list