[Edu-sig] Re: [Tutor] Thoughts on little lambda

Kirby Urner urnerk@qwest.net
Mon, 11 Mar 2002 14:07:37 -0800


At 12:11 PM 3/11/2002 -0800, Danny Yoo wrote:

> > A brief experiment in the interpreter seems to indicate that a
> > function's __doc__ is assignable, so it should be possible, after
> > defining the function but before returning it, to do
> >
> >     main.__doc__ = "This function returns %s" % descripton
> >
> > or something similar.
>
>
>Here's a concrete example of this:
>
>###
> >>> def makeAdder(n):
>...     def function(x):
>...         "This function adds %(n)s to x."
>...         return n + x
>...     function.__doc__ = function.__doc__ % {'n' : n}
>...     return function
>...
> >>> f = makeAdder(42)
> >>> f.__doc__
>'This function adds 42 to x.'
>###
>

Or even just:

  >>> def makeAdder(n):
        def function(x):
            return n + x
        function.__doc__ = "This function adds %s to x." % n
        return function

  >>> add42 = makeAdder(42)
  >>> help(add42)
  Help on function function:

  function(x)
      This function adds 1 to x.

Kirby