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

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 11 Mar 2002 12:11:03 -0800 (PST)


On Mon, 11 Mar 2002, Jeff Shannon wrote:

> 
> 
> Kirby Urner wrote:
> 
> >
> > Next question:  Is there a way to associate a customized
> > doc string with the built function?
> 
> 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.'
###