Re: triple quoted strings as comments
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: triple quoted strings as comments

From: Terry Hancock <hancock@anansispaceworks.com>
Date: Tue Jan 31 2006 - 03:04:09 CET

On 30 Jan 2006 16:29:15 -0800
"dmh2000" <dmh2000@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@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com
Received on Tue Feb 7 20:18:21 2006