triple quoted strings as comments

Peter Hansen peter at engcorp.com
Mon Jan 30 20:38:45 EST 2006


dmh2000 wrote:
> I recently complained elsewhere that Python doesn't have multiline
> comments. i was told to use triple quoted strings to make multiline
> comments. My question is that since a triple quoted string is actually
> a language construct, does it use cause a runtime construction of a
> string which is then discarded, or is the runtime smart enough to see
> that it isn't used and so it doesn't construct it?

Easy enough to find out.  Put this in test.py:

'first module comment'
'another'

def func():
     'first func comment'
     'another'
     print 'hi'
     'last one'

'and last module comment'


Now do "import test" from the Python prompt, then exit the interpreter 
and do "strings test.pyc" on the compiled bytecode.

(The short answer is only the first such comments are kept.)

-Peter




More information about the Python-list mailing list