Extract doc strings

Discussion in 'Python' started by moerchendiser2k3, Jul 1, 2010.

  1. Hi all,

    when I have a PyCodeObject, is there any way to extract a doc string
    from the code?
    For instance the following script

    <---script---->
    """
    Hello World!
    """
    def main():
    pass
    main()
    </---script--->

    I would like to get "Hello World!". Any chance? Thanks in advance!!

    Bye, moerchendiser2k3
    moerchendiser2k3, Jul 1, 2010
    #1
    1. Advertising

  2. Hi ben,

    thanks for your answer. Sure, __doc__ is the access point to the
    docstring, but in this case, I just get the docstring of the code
    object. So an explanation what a code object, but I need the docstring
    of real docstring of the script.

    bye, moerchendiser
    moerchendiser2k3, Jul 1, 2010
    #2
    1. Advertising

  3. moerchendiser2k3 wrote:
    > Hi all,
    >
    > when I have a PyCodeObject, is there any way to extract a doc string
    > from the code?
    > For instance the following script
    >
    > <---script---->
    > """
    > Hello World!
    > """
    > def main():
    > pass
    > main()
    > </---script--->
    >
    > I would like to get "Hello World!". Any chance? Thanks in advance!!
    >
    > Bye, moerchendiser2k3
    >

    Hello,


    """Hello world!!"""

    import sys
    def main():
    print sys.modules[__name__].__doc__

    main()



    JM
    Jean-Michel Pichavant, Jul 1, 2010
    #3
  4. On 07/01/2010 11:12 AM, moerchendiser2k3 wrote:
    > Hi all,
    >
    > when I have a PyCodeObject, is there any way to extract a doc string
    > from the code?
    > For instance the following script


    Code objects don't have doc strings, as such. However, for functions at
    least (this may or may not be true for code originating elsewhere), it
    appears that the code's first constant is always the docstring, even if
    it's None:


    >>> def f(a=()):

    .... """doc string"""
    .... return 1,2,3,a
    ....
    >>> def g(a=[], b=0):

    .... return False
    ....
    >>> f.__code__.co_consts

    ('doc string', 1, 2, 3)
    >>> g.__code__.co_consts

    (None, False)
    >>>


    Why else would g.__code__.co_consts[0] be None if that wasn't a
    docstring-specific special place. I don't know how you're creating your
    codeobject, and I don't know if this holds for your case.

    -- Thomas

    >
    > <---script---->
    > """
    > Hello World!
    > """
    > def main():
    > pass
    > main()
    > </---script--->


    This looks like something that should usually be accessed as a module,
    and in that case, it's trivial, as others have pointed out.
    Thomas Jollans, Jul 1, 2010
    #4
  5. On Thu, 01 Jul 2010 11:55:05 +0200, Thomas Jollans wrote:

    > On 07/01/2010 11:12 AM, moerchendiser2k3 wrote:
    >> Hi all,
    >>
    >> when I have a PyCodeObject, is there any way to extract a doc string
    >> from the code?
    >> For instance the following script

    >
    > Code objects don't have doc strings, as such. However, for functions at
    > least (this may or may not be true for code originating elsewhere), it
    > appears that the code's first constant is always the docstring, even if
    > it's None:



    Nope, you're misinterpreting what you're seeing. Here's a counter-example
    from Python 3.1:

    >>> def f():

    .... 999
    .... return 42
    ....
    >>> f.__code__.co_consts

    (None, 42)


    The docstring is None, unless the function definition is immediately
    followed by a string literal, in which case the string literal is taken
    as the docstring. Any other object is removed by the keyhole optimizer:

    >>> import dis
    >>> dis.dis(f)

    3 0 LOAD_CONST 1 (42)
    3 RETURN_VALUE

    In older versions of Python, the keyhole optimizer was less clever.
    Here's an example from 2.4:

    >>> def f():

    .... 999
    .... return 42
    ....
    >>> f.func_code.co_consts

    (None, 999, 42)
    >>> import dis
    >>> dis.dis(f)

    2 0 LOAD_CONST 1 (999)
    3 POP_TOP

    3 4 LOAD_CONST 2 (42)
    7 RETURN_VALUE


    Aside: Python's keyhole optimizer has removed unreachable string
    constants (other than doc strings) at least since Python 1.5. But doing
    the same to other object types had to wait until Python 2.5.



    --
    Steven
    Steven D'Aprano, Jul 1, 2010
    #5
  6. Great! Thanks a lot!

    This is what I was looking for. :)

    Bye, moerchendiser2k3
    moerchendiser2k3, Jul 1, 2010
    #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. John Wallace
    Replies:
    0
    Views:
    456
    John Wallace
    Jul 22, 2003
  2. Matt
    Replies:
    3
    Views:
    479
    Tor Iver Wilhelmsen
    Sep 17, 2004
  3. Praveen Chhangani

    Converting a org.jdom DOC to org.w3c DOC

    Praveen Chhangani, Aug 5, 2003, in forum: XML
    Replies:
    2
    Views:
    948
    Johannes Koch
    Aug 7, 2003
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    733
    Malcolm
    Jun 24, 2006
  5. News123
    Replies:
    0
    Views:
    284
    News123
    Feb 6, 2010
Loading...

Share This Page