Extract doc strings

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Jul 1 08:16:49 EDT 2010


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



More information about the Python-list mailing list