[Tutor] nested function definition

Norvell Spearman norvell@houseofspearman.org
Tue Apr 29 03:38:02 2003


In the Python tutorial, section 5.1.3, ``Functional Programming Tools,''
is the following code:

    >>> def sum(seq):
    ...     def add(x,y): return x+y
    ...     return reduce(add, seq, 0)
    ...
    >>> sum(range(1, 11))
    55
    >>> sum([])
    0

Which are the advantages or disadvantages (if any) of defining one
function in the definition of another?  Defining add(x, y) outside the
definition of sum(seq) doesn't appear to change the behavior of either.
Thanks for any answers.

-- 
Norvell Spearman