How to comment constant values?

Robert Kern robert.kern at gmail.com
Mon Jul 27 00:16:24 EDT 2009


On 2009-07-26 18:23, Steven D'Aprano wrote:
> On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote:
>
>>> Only modules, classes, and functions/methods can have docstrings
>>> associated with them.
>>> For anything else, you have to use comments; or you can mention them in
>>> the docstrings of related things.
>> While this is technically true, writing docstrings to constants (module
>> or classlevel) works when one uses tools such as epydoc to generate
>> documentation.
>
> I've never used epydoc, so I'm not sure what you mean. Presumably it uses
> source code analysis to detect:
>
> CONSTANT = 42
> """This is a constant."""
>
> even though the string is ignored by the compiler.
>
> Is that correct?

epydoc 3 can actually parse the file to grab comments with a "#:" marker:

#: This is a constant.
CONSTANT = 42

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list