triple quoted strings as comments

Terry Hancock hancock at anansispaceworks.com
Mon Jan 30 21:04:09 EST 2006


On 30 Jan 2006 16:29:15 -0800
"dmh2000" <dmh2000 at gmail.com> 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?
> 
> example
> 
> def fun(self):
>   """doc comment
>       comment line 2
>   """

This is specifically a "docstring" so it remains attached as
an attribute of fun: fun.__doc__

>   x = 1
>   y = 2
> 
>   """does this triple quoted string used as a  comment
>       cause something to happen at runtime beyond
>       just skipping over it? Such as allocation of memory
>       for a string or worse yet garbage collection? or
>       not?
>   """

This string is really unused. It will produce a value when
processed the first time, but it's not bound so it gets
immediately garbage-collected. And it won't be there after
the module is byte-compiled. So, you lose a little time the
very first time the file is used (but that's technically
true for a regular comment too -- I think this loses you a
little more time).  But it's pretty trivial in practice,
because every subsequent time, it's gone.

>   z = x + y


At least, this is how I understand it.

Cheers,
Terry


-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list