Function docstring as a local variable

T

Tim Johnson

Consider the following:
## code
def test():
"""This is my docstring"""
print(??) ## can I print the docstring above?
## /code
It possible for a function to print it's own docstring?

thanks
(pointers to docs could be sufficient)
 
C

Corey Richardson

Excerpts from Colin J. Williams's message of Sun Jul 10 18:28:15 -0400 2011:
Try:

def f():
ds= """docstring"""
print ds

That doesn't actually make a docstring, though. It makes a string object and
points the name ds at it. Do you know what a docstring is?

def foo():
"""I am a docstring"""
pass

def bar():
ds = "I am not a docstring!"

def baz():
"I am a docstring too!"
pass

def qux():
'And even me! Quote type don't matter (besides style)'
pass
--
Corey Richardson
"Those who deny freedom to others, deserve it not for themselves"
-- Abraham Lincoln

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)

iQEcBAEBCAAGBQJOGi7/AAoJEAFAbo/KNFvpQ6AH+gMFXBda/VwzmbdUBJYLTU00
/X2iqiN10sejZNnSvLAd02vEXbT2FPNy54X1ehNyNhBVre4JslaeZK+6Z+ruzHvA
GRegRofTDobLR0GWUKvDLk4369+80IA22gJlbZpYmYwTjDh5gkJchdJ9cGDk5ZXW
hgNsav6qFpW7ok37/zVY14ljGtY3gOIR9yHHhnnoiZVRK5leWWlfk2JTpuvTNuCX
znL6C0bvmealq1VChEr/LNtmq1Q8wnyUy3qmhMiHs++suJbmpcJrtiyc9pyjVpee
vhWSzH2L60pgLqZDA8A/eN0ni8/8jvcwnBpwJbS+Gsuw9cQo8w88pruU6mB3scA=
=zsEq
-----END PGP SIGNATURE-----
 
S

Steven D'Aprano

Excerpts from Colin J. Williams's message of Sun Jul 10 18:28:15 -0400
2011:

That doesn't actually make a docstring, though. It makes a string object
and points the name ds at it. Do you know what a docstring is?


Colin might not be aware of why docstrings are useful, and arbitrary local
variables ds="""docstring""" are not.


def f1():
"""This is my documentation string.

Imagine this has actual useful information.
"""

def f2():
dc = """This is my documentation string.

Imagine this has actual useful information.
"""


Now, at the interactive interpreter, call:

help(f1)
help(f2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top