473,378 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

triple quoted strings as comments

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

Jan 31 '06 #1
14 6082
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

Jan 31 '06 #2
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

Jan 31 '06 #3
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é

Jan 31 '06 #4
On 30 Jan 2006 16:29:15 -0800
"dmh2000" <dm*****@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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Jan 31 '06 #5
"dmh2000" <dm*****@gmail.com> 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?
Jan 31 '06 #6
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/

Jan 31 '06 #7
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)
Jan 31 '06 #8
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
Jan 31 '06 #9
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!
Feb 1 '06 #10
Magnus Lycka <ly***@carmen.se> wrote:
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!


/* */ also allows for some truly spectacularly bad coding practices. Not
long ago, I ran into some real-life code where a previous developer had
commented out about 50 lines of C++ code by just putting a /* at the top
and a */ at the bottom. I was tearing my hair out trying to figure out how
the code worked until I noticed what he had done.
Feb 1 '06 #11
Roy Smith schreef:
Magnus Lycka <ly***@carmen.se> wrote:
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!


/* */ also allows for some truly spectacularly bad coding practices. Not
long ago, I ran into some real-life code where a previous developer had
commented out about 50 lines of C++ code by just putting a /* at the top
and a */ at the bottom. I was tearing my hair out trying to figure out how
the code worked until I noticed what he had done.


That happened to me a few times. These days I use an editor with syntax
highlighting that shows comments in another color than code. That helps
tremendously.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Feb 1 '06 #12
On Wed, 01 Feb 2006 13:41:33 GMT, Roel Schroeven <rs****************@fastmail.fm> wrote:
Roy Smith schreef:

....
/* */ also allows for some truly spectacularly bad coding practices. Not
long ago, I ran into some real-life code where a previous developer had
commented out about 50 lines of C++ code by just putting a /* at the top
and a */ at the bottom. I was tearing my hair out trying to figure out how
the code worked until I noticed what he had done.


That happened to me a few times. These days I use an editor with syntax
highlighting that shows comments in another color than code. That helps
tremendously.


Syntax highlighting, same here. Plus version control so you can see who did
it, and make a mental note not to trust that person in the future ;-)

("#if 0" in C and C++ are better choices, but only marginally. Best is to
remove the code unless you are sure it's needed again soon. Works in all
languages.)

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
Feb 5 '06 #13
Jorgen Grahn wrote:
[...] developer had
commented out about 50 lines of C++ code by just putting a /* at the top
and a */ at the bottom.
[...] ("#if 0" in C and C++ are better choices, but only marginally. Best is to
remove the code unless you are sure it's needed again soon. Works in all
languages.)


However, I'd only advise to do this if you are using a revision control.
Otherwise, you'll end up having a lot of "backup" files hanging around
which are even worse than multi-row comments. Or, even worse: If you
don't create backup files before removing code ...
Greets,

Volker

--
Volker Grabsch
---<<(())>>---
\frac{\left|\vartheta_0\times\{\ell,\kappa\in\Re\} \right|}{\sqrt
[G]{-\Gamma(\alpha)\cdot\mathcal{B}^{\left[\oint\!c_\hbar\right]}}}
Feb 6 '06 #14
On 6 Feb 2006 12:53:53 GMT, Volker Grabsch <vo************@v.notjusthosting.com> wrote:
Jorgen Grahn wrote:
[...] developer had
commented out about 50 lines of C++ code by just putting a /* at the top
and a */ at the bottom.

[...]
("#if 0" in C and C++ are better choices, but only marginally. Best is to
remove the code unless you are sure it's needed again soon. Works in all
languages.)


However, I'd only advise to do this if you are using a revision control.


But surely, everybody's using revision control these days?
(Feel free to smile at my naivety now ;-)

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
Feb 6 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Edward C. Jones | last post by:
On 2003-09-04, Rasmus Fogh said: > I need a way of writing strings or arbitrary Python code that will > > a) allow the strings to be read again unchanged (like repr) > b) write multiline...
11
by: bearophile | last post by:
Hello, here are a four more questions (or suggestions) for the language (probably people have already discussed some of/all such things: I've seen the contracts for Python:...
5
by: Gary McCullough | last post by:
What I want to do sounds simple, but it's defeating me. I want to substitute all occurences of a colon : character in a string with an @ character -- unless the : occurs within a single or...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
4
by: erikjalevik | last post by:
I have a long string of quoted strings, like: "string 1" "string 2" ... and I need to split this up into the constituent quoted strings. I was thinking it would be nice if I could somehow put...
7
by: p.lavarre | last post by:
Subject: announce: FAQs suggested ... That suggested FAQ is misleadingly incorrect as stated - we need help rewording it. /F correctly commented: "eval" is never a good choice if you cannot...
8
by: Lawrence D'Oliveiro | last post by:
If triple-quoted strings had the Python-nature, then they would take indentation into account. Thus: """this is a multi-line string.""" would be equivalent to
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.