Function docstring as a local variable

Discussion in 'Python' started by Tim Johnson, Jul 10, 2011.

  1. Tim Johnson

    Tim Johnson Guest

    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)
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
    Tim Johnson, Jul 10, 2011
    #1
    1. Advertising

  2. Tim Johnson

    rantingrick Guest

    On Jul 10, 12:41 pm, Tim Johnson <> wrote:
    > It possible for a function to print it's own docstring?


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

    any questions?
    rantingrick, Jul 10, 2011
    #2
    1. Advertising

  3. On 10-Jul-11 13:44 PM, rantingrick wrote:
    > On Jul 10, 12:41 pm, Tim Johnson<> wrote:
    >> It possible for a function to print it's own docstring?

    >
    > def f():
    > """docstring"""
    > print "docstring"
    >
    > any questions?


    Try:

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


    Colin W.
    Colin J. Williams, Jul 10, 2011
    #3
  4. 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-----
    Corey Richardson, Jul 11, 2011
    #4
  5. On Mon, 11 Jul 2011 09:00 am Corey Richardson wrote:

    > 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?



    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)



    --
    Steven
    Steven D'Aprano, Jul 11, 2011
    #5
  6. Tim Johnson

    alex23 Guest

    On Jul 11, 9:00 am, Corey Richardson <> wrote:
    > def qux():
    >     'And even me! Quote type don't matter (besides style)'


    Well, style and the presence of the string literal notation in the
    quoted text :)
    alex23, Jul 11, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sairam
    Replies:
    2
    Views:
    364
    Steve Holden
    Apr 5, 2007
  2. bdb112
    Replies:
    3
    Views:
    261
    Chris Rebert
    Mar 9, 2009
  3. Chris Angelico

    Re: Function docstring as a local variable

    Chris Angelico, Jul 10, 2011, in forum: Python
    Replies:
    8
    Views:
    221
    Tim Johnson
    Jul 11, 2011
  4. Carl Banks
    Replies:
    5
    Views:
    188
    alex23
    Jul 11, 2011
  5. Arnaud Delobelle
    Replies:
    0
    Views:
    198
    Arnaud Delobelle
    Sep 15, 2011
Loading...

Share This Page