Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > triple quoted strings as comments

Reply
Thread Tools

triple quoted strings as comments

 
 
dmh2000
Guest
Posts: n/a
 
      01-31-2006
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
"""

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?
"""
z = x + y

....

dave howard

 
Reply With Quote
 
 
 
 
mithrond@gmail.com
Guest
Posts: n/a
 
      01-31-2006
as i know, the triple quoted string does cause a runtime construction,
and will not be discarded, and it's a benefit of python language.
here is sth. useful.
_http://diveintopython.org/power_of_introspection/index.html

best regard

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      01-31-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

 
Reply With Quote
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      01-31-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?
>
> example
>
> def fun(self):
> """doc comment
> comment line 2
> """
>
> 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?
> """
> z = x + y

It seems to discard the second triple quoted comment (the first one is
kept around as a doc string).
I created two scripts, one with the second triple quoted string, the
other without. The compiled version is *almost* the same (one byte
difference which, if I am not mistaken, comes from the different
filename embedded in the .pyc file).

30/01/2006 09:34 PM 327 triple.py
30/01/2006 09:35 PM 359 triple.pyc
30/01/2006 09:34 PM 96 triple2.py
30/01/2006 09:35 PM 358 triple2.pyc
André

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      01-31-2006
"dmh2000" <(E-Mail Removed)> wrote:
> I recently complained elsewhere that Python doesn't have multiline
> comments.


Of course it has multi-line comments. They look like this:

# This is the first line
# and this is the second.

Why is that a problem?
 
Reply With Quote
 
Terry Hancock
Guest
Posts: n/a
 
      01-31-2006
On 30 Jan 2006 16:29:15 -0800
"dmh2000" <(E-Mail Removed)> 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 ((E-Mail Removed))
Anansi Spaceworks http://www.AnansiSpaceworks.com

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      01-31-2006
dmh2000 wrote:
> I recently complained elsewhere that Python doesn't have multiline
> comments.


Personally I think it's a win that you couldn't find anything more
serious to complain about

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      01-31-2006
dmh2000 wrote:

> example
>
> def fun(self):
> """doc comment
> comment line 2
> """
>
> 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?
> """
> z = x + y
>


How to find out for yourself:

>>> def fun(self):

"""doc comment
comment line 2
"""

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?
"""
z = x + y


>>> import dis
>>> dis.dis(fun)

6 0 LOAD_CONST 1 (1)
3 STORE_FAST 2 (x)

7 6 LOAD_CONST 2 (2)
9 STORE_FAST 1 (y)

14 12 LOAD_FAST 2 (x)
15 LOAD_FAST 1 (y)
18 BINARY_ADD
19 STORE_FAST 3 (z)
22 LOAD_CONST 3 (None)
25 RETURN_VALUE
>>>


Further inspection shows that it hasn't even saved that second string as a
constant:

>>> print fun.func_code.co_consts

('doc comment\n comment line 2\n ', 1, 2, None)
 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      01-31-2006
Steve Holden wrote:
> dmh2000 wrote:
>
>> I recently complained elsewhere that Python doesn't have multiline
>> comments.

>
>
> Personally I think it's a win that you couldn't find anything more
> serious to complain about


+1 QOTW
 
Reply With Quote
 
Magnus Lycka
Guest
Posts: n/a
 
      02-01-2006
dmh2000 wrote:
> I recently complained elsewhere that Python doesn't have multiline
> comments.

It seems you have a bad editor if it can't conveniently
add and remove comment markers for arbitrary blocks in
your source. (Maybe you just didn't find this feature.)

That every comment line begins with a special symbol just
adds clarity. It's a bonus. C++ introduced // as comment
symbol for this purpose (but was stuck with /* and */ due
to backward compatibility reasons). Ada, while otherwise
similar to Pascal, adopted -- to end of row instead of (*
and *) etc.

An editor that adds/removes '# ' in the beginning of each
marked line is fairly bullet proof. Adding e.g. /* to the
beginning of a block you want to comment out, & */ to the
end, breaks if you have /* */ style comments in the block!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple quoted string in exec function ? Stef Mientki Python 0 12-30-2008 01:42 PM
Triple-quoted strings hath not the Python-nature Lawrence D'Oliveiro Python 8 10-27-2008 02:50 PM
FW: Escaping a triple quoted string' newbie question Jules Stevenson Python 1 03-02-2008 10:57 AM
pydoc preference for triple double over triple single quotes -- anyreason? Brian van den Broek Python 7 04-19-2005 03:39 AM
Triple quoted repr Edward C. Jones Python 1 06-03-2004 06:44 PM



Advertisments