[Tutor] assigning __doc__ strings (manually)

Kent Johnson kent_johnson at skillsoft.com
Wed Sep 1 17:31:38 CEST 2004


You can assign f.__doc__ after f is defined:
 >>> def real_f(x):
...     """this is my doc-string"""
...     return x**2, x**3
...
 >>> def f(x):
...     return real_f(x)[0]
...
 >>> f.__doc__ = real_f.__doc__
 >>> f.__doc__
'this is my doc-string'

Kent

At 04:21 PM 9/1/2004 +0100, Hans Fangohr wrote:
>Greetings,
>
>I am facing the following problem and would like some advise:
>
>I have two functions, say for simplicity, real_f and f:
>
>def real_f(x):
>     """this is my doc-string"""
>     return x**2, x**3
>
>def f(x):
>     return real_f(x)[0]
>
>
>The actual work is done in real_f() and some users might be interested
>in x**3 and x**2. However, most of the users will only care about x**2
>and are happy to use the function f() for this.
>
>(In the actual program I am working on there is of course more work
>involved than just computing x**2 and x**3 ...)
>
>Since the main computation is done in real_f(), I thought I'd create a
>doc-string in real_f() that explains the computation being implemented
>etc (as done above).
>
>The function f() does basically the same as real_f() and should
>provide the same docstring as real_f(). To avoid copying the
>information, I thought I could do this when defining f():
>
>def f(x):
>     real_f.__doc__
>
>     return real_f(x)[0]
>
>
>which -- I was hoping -- would provide real_f.__doc__ in
>f.__doc__. However, f.__doc__ is None.
>
>Can this be solved? Or am I on the wrong track here?  Looking forward
>to hearing from you,
>
>Hans
>
>
>
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list