Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87687 > unrolled thread
| Started by | Aditya Raj Bhatt <adityarajbhatt@gmail.com> |
|---|---|
| First post | 2015-03-18 10:46 -0700 |
| Last post | 2015-03-22 12:00 +0200 |
| Articles | 20 on this page of 56 — 17 participants |
Back to article view | Back to comp.lang.python
A simple single line, triple-quoted comment is giving syntax error. Why? Aditya Raj Bhatt <adityarajbhatt@gmail.com> - 2015-03-18 10:46 -0700
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Laurent Pointal <laurent.pointal@laposte.net> - 2015-03-18 19:04 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Laurent Pointal <laurent.pointal@laposte.net> - 2015-03-18 19:06 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-18 20:53 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-18 20:56 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Terry Reedy <tjreedy@udel.edu> - 2015-03-18 17:47 -0400
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-19 09:58 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Chris Angelico <rosuav@gmail.com> - 2015-03-19 10:23 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Aditya Raj Bhatt <adityarajbhatt@gmail.com> - 2015-03-18 11:23 -0700
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Igor Korot <ikorot01@gmail.com> - 2015-03-18 14:45 -0400
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Laurent Pointal <laurent.pointal@laposte.net> - 2015-03-18 20:08 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Tim Roberts <timr@probo.com> - 2015-03-28 16:34 -0700
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-18 21:21 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Tim Chase <python.list@tim.thechases.com> - 2015-03-18 13:15 -0500
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ben Finney <ben+python@benfinney.id.au> - 2015-03-19 11:06 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-19 00:53 +0000
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-22 04:14 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Chris Angelico <rosuav@gmail.com> - 2015-03-22 14:36 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-22 04:49 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Chris Angelico <rosuav@gmail.com> - 2015-03-22 15:11 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-26 05:35 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-25 23:09 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-26 17:45 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-26 12:29 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-26 19:54 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-26 17:27 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-28 19:20 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-29 01:56 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-29 12:41 +0200
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-31 01:23 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? sohcahtoa82@gmail.com - 2015-03-31 16:10 -0700
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Chris Angelico <rosuav@gmail.com> - 2015-04-01 10:36 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-31 17:43 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-04-02 23:31 +0200
Re: A simple single line, triple-quoted comment is giving syntax error. Why? sohcahtoa82@gmail.com - 2015-04-02 15:26 -0700
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-02 18:21 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-03 16:40 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Chris Angelico <rosuav@gmail.com> - 2015-04-03 16:57 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Marko Rauhamaa <marko@pacujo.net> - 2015-04-03 10:13 +0300
r"\"" ??? (was A simple single line, triple-quoted comment) Rustom Mody <rustompmody@gmail.com> - 2015-04-03 05:52 -0700
Re: r"\"" ??? (was A simple single line, triple-quoted comment) Chris Angelico <rosuav@gmail.com> - 2015-04-04 01:40 +1100
Re: r"\"" ??? (was A simple single line, triple-quoted comment) Rustom Mody <rustompmody@gmail.com> - 2015-04-03 08:14 -0700
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-03 09:25 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-03 00:40 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-03-27 11:43 +1300
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-28 19:06 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-26 12:32 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-26 19:56 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-27 10:15 +1100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Tim Chase <python.list@tim.thechases.com> - 2015-03-26 18:47 -0500
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-26 19:38 -0600
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Marko Rauhamaa <marko@pacujo.net> - 2015-03-27 07:34 +0200
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Dave Angel <davea@davea.name> - 2015-03-26 01:23 -0400
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-26 18:25 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-26 18:30 +0100
Re: A simple single line, triple-quoted comment is giving syntax error. Why? Marko Rauhamaa <marko@pacujo.net> - 2015-03-22 12:00 +0200
Page 1 of 3 [1] 2 3 Next page →
| From | Aditya Raj Bhatt <adityarajbhatt@gmail.com> |
|---|---|
| Date | 2015-03-18 10:46 -0700 |
| Subject | A simple single line, triple-quoted comment is giving syntax error. Why? |
| Message-ID | <f7b84d9d-c559-4711-ad63-1deea5f12c62@googlegroups.com> |
I always do single line comments with # but just for the sake of it I tried it with ''' ''' and it gives me a syntax error. In both the interpreter, and the source code text file, doing - a = 5 '''a comment''' results in a syntax error, with the very last quote at the end of the line highlighted in red. Of course, if I do - a = 5 #'''a comment''' it works. I searched for the problem, and arrived at http://stackoverflow.com/questions/397148/why-doesnt-python-have-multiline-comments which says that there are no 'true' multiline comments in python and that all those 'block' comments are actually triple-quoted strings. Then I looked in the documentation and found https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals but it is a little bit too complex for my understanding (I'm just starting python). So can someone tell me why a triple-quoted string gives a syntax error if only in one line? Actually, there are other confusions I have too, regarding using backslashes inside triple-quoted strings to form multi-line comments, and a general uncertainty about triple-quoted strings. Can someone also provide a sort of a 'guide' to triple-quoted comments in general? Something like how I can just sum up index slices by saying in [a:b], the 'counting' for a always starts with 0, a is included, everything up to b but not b is included (assuming this is in fact the correct explanation ;-))
[toc] | [next] | [standalone]
| From | Laurent Pointal <laurent.pointal@laposte.net> |
|---|---|
| Date | 2015-03-18 19:04 +0100 |
| Message-ID | <5509be2c$0$3027$426a74cc@news.free.fr> |
| In reply to | #87687 |
Aditya Raj Bhatt wrote:
> I always do single line comments with # but just for the sake of it I
> tried it with ''' ''' and it gives me a syntax error.
>
> In both the interpreter, and the source code text file, doing -
>
> a = 5 '''a comment'''
>
> results in a syntax error, with the very last quote at the end of the
> line highlighted in red. Of course, if I do -
>
> a = 5 #'''a comment'''
<zip>
> Can someone also provide a sort of a 'guide' to triple-quoted comments
> in general?
A triple ' or " string is a Python string, allowing line-return in string.
If it is in an expression (like a = 5 '''a comment'''), then it must be a
valid expression (and here it is not).
You can have an expression alone, with no stored result (ie. no assignment
to a name), and this is how are used triple quoted strings as comments.
a = 5
"""a comment"""
Take care of indent:
def f(x):
a = 5
"""an correctly indented expression to be
inside the function"""
return a * x
A+
Laurent.
[toc] | [prev] | [next] | [standalone]
| From | Laurent Pointal <laurent.pointal@laposte.net> |
|---|---|
| Date | 2015-03-18 19:06 +0100 |
| Message-ID | <5509beaf$0$3027$426a74cc@news.free.fr> |
| In reply to | #87688 |
Laurent Pointal wrote: (oups) > Take care of indent: > > def f(x): > a = 5 > """an correctly indented expression to be > inside the function""" > return a * x Here only the first indent of """ at beginning of the string to be aligned to function bloc is important, remaining content of the string can be indented or not.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-18 20:53 +0100 |
| Message-ID | <23392839.THbznS7tis@PointedEars.de> |
| In reply to | #87690 |
Laurent Pointal wrote: > Laurent Pointal wrote: >> Take care of indent: >> >> def f(x): >> a = 5 >> """an correctly indented expression to be >> inside the function""" >> return a * x > > Here only the first indent of """ at beginning of the string to be aligned > to function bloc is important, remaining content of the string can be > indented or not. I must strongly object and recommend against getting accustomed to the suggested use of multi-line string literals. First of all, it is too easy to produce invalid code this way. Also, standalone multi-line string literals in Python code serve a specific purpose when at the beginning of a function, method or class clock: they constitute *docstrings”, setting the respective object’s “__doc__” attribute: >>> def f(x): ... """Returns a multiple of 5""" ... a = 5 ... return a * x ... >>> print(f.__doc__) Returns a multiple of 5 This means that different to real comments, the content of string literals is *parsed*. You do not want to waste the time of the compiler compiling code that serves no purpose; particularly not in a language like Python where code is JIT-compiled by default. Not any less important: Using standalone multi-line string literals in any other way is syntactically correct indeed, but makes the code harder to read. Keep in mind that code may be written only once, but read many times by other people during its lifetime. It is important to write code so that it is easy to read, so that it can be easily reused and adapted by oneself and others (a month, a year later). If you want to comment your code, then by all means do so using docstrings and *real* comments. IDEs like PyDev support you there. That way also the syntax highlighting will be correct. And with real comments, the Python compiler does not care about the indentation. As to where you should put comments, I recommend in general, in all programming languages, if possible to put the comments on their own line, *before* the statement that they are describing, and using the same indentation, preferably preceded by an empty line (so that the comment and the code it documents stand out). <https://docs.python.org/3/tutorial/controlflow.html#documentation-strings> <https://docs.python.org/3/reference/lexical_analysis.html#line-structure> -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-18 20:56 +0100 |
| Message-ID | <1572008.KLgTz3aA73@PointedEars.de> |
| In reply to | #87696 |
Thomas 'PointedEars' Lahn wrote:
> […] standalone multi-line string literals in Python code serve a specific
> purpose when at the beginning of a function, method or class clock: they
^ block¹
> constitute *docstrings”, setting
_____
¹ Apparently I am doing to much relativity lately ;-)
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2015-03-18 17:47 -0400 |
| Message-ID | <mailman.10.1426715280.10327.python-list@python.org> |
| In reply to | #87696 |
On 3/18/2015 3:53 PM, Thomas 'PointedEars' Lahn wrote: > I must strongly object and recommend against getting accustomed to the > suggested use of multi-line string literals. I agree. > If you want to comment your code, then by all means do so using docstrings > and *real* comments. IDEs like PyDev support you there. For instance, with Idle, before (with red comment coloring): # this is # a very very very ragged ragged ragged ragged ragged ragged ragged # multiline comment # that I will reformat # with Idle's Format Paragraph (default key binding alt-Q, but this may be reset by the user as long as the user is careful to not resuse another key. after alt-q anywhere in the comment block, with default 72 char lines # this is a very very very ragged ragged ragged ragged ragged ragged # ragged multiline comment that I will reformat with Idle's Format # Paragraph (default key binding alt-Q, but this may be reset by the # user as long as the user is careful to not resuse another key. > That way also the syntax highlighting will be correct. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-03-19 09:58 +1100 |
| Message-ID | <550a0319$0$12999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #87696 |
On Thu, 19 Mar 2015 06:53 am, Thomas 'PointedEars' Lahn wrote: > I must strongly object and recommend against getting accustomed to the > suggested use of multi-line string literals. I agree in general with Thomas' comments about using arbitrary strings as comments. If you want to write code according to *best practices*, then you should not use strings as comments. But let's also remember that sometimes you cannot be bothered with *best* practice, and "good enough for now" practice is fine. Need to comment out a large block of code in a hurry while using a basic editor that doesn't offer a "Comment" and "Uncomment" command? Best practice says I should use a better editor, and that's fine, but right this instant I'm using the tools I have, not the tools I want, and the easiest way to fix this is to surround the block of code with triple-quotes. I can always come back and fix it later. Famous last words, I know, but who among us can truthfully say they have *never* taken a quick and dirty short-cut while programming? I also find it convenient and unobjectionable to include a bare triple quoted string at the very end of the file, as a sort of "note pad" area for me to write notes to myself while the work is in early stages of progress. I can put in small code snippets, URLs to pages I need to look at, general comments, etc. without having to care about leading # signs. And because I don't use strings for comments anywhere else, this note pad area stands out and reminds me to remove it before releasing the code into production. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-03-19 10:23 +1100 |
| Message-ID | <mailman.13.1426720990.10327.python-list@python.org> |
| In reply to | #87706 |
On Thu, Mar 19, 2015 at 9:58 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > But let's also remember that sometimes you cannot be bothered with *best* > practice, and "good enough for now" practice is fine. > > Need to comment out a large block of code in a hurry while using a basic > editor that doesn't offer a "Comment" and "Uncomment" command? Best > practice says I should use a better editor, and that's fine, but right this > instant I'm using the tools I have, not the tools I want, and the easiest > way to fix this is to surround the block of code with triple-quotes. I can > always come back and fix it later. Famous last words, I know, but who among > us can truthfully say they have *never* taken a quick and dirty short-cut > while programming? There are three broad types of comments: 1) Single-line or to-end-of-line comments, started with a # or a // or something. Python supports these, they're easy to work with and recognize. No problem. 2) Block comments, consisting of a number of entire lines of either text or commented-out code. 3) Partial-line comments - /* comment */ in C-like languages. You can abuse triple-quoted strings to do #2, but not #3. For instance, in C, you can legally do something like this: if (condition1 /* && condition2 */) do_stuff(); In terms of "quick and dirty short-cuts", that's one that I miss having a mechanic for in Python. And yes, you can decry it as a bad practice, but just as with Steven's example, it's always with the plan to fix it later (usually by making the code more flexible in some way), and yet does sometimes stay in the code for a rather long time, for the same reason that several of my Alice posters are currently lying face down on the Alice Shelf instead of being up on the wall - there's nothing more permanent than a temporary solution. (There's also technically a fourth use of slash-star comments - beginning part way into one line and ending part way into another line: code code /* blah blah blah blah blah */ more code But if you do this on a regular basis, I think we are justified in throwing rotten fruit at you.) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Aditya Raj Bhatt <adityarajbhatt@gmail.com> |
|---|---|
| Date | 2015-03-18 11:23 -0700 |
| Message-ID | <8908fd12-9069-4230-8b1c-3f88e74bdabf@googlegroups.com> |
| In reply to | #87688 |
On Wednesday, March 18, 2015 at 1:04:39 PM UTC-5, Laurent Pointal wrote: > > Can someone also provide a sort of a 'guide' to triple-quoted comments > > in general? > > A triple ' or " string is a Python string, allowing line-return in string. What do you mean by line-return in string? Is it newline? Does it mean I can write - '''first part second part''' ? > If it is in an expression (like a = 5 '''a comment'''), then it must be a > valid expression (and here it is not). What is not a valid expression here? ><snip> > A+ > Laurent. Thank you for the fast and detailed response, Aditya
[toc] | [prev] | [next] | [standalone]
| From | Igor Korot <ikorot01@gmail.com> |
|---|---|
| Date | 2015-03-18 14:45 -0400 |
| Message-ID | <mailman.6.1426704320.10327.python-list@python.org> |
| In reply to | #87692 |
Hi, On Wed, Mar 18, 2015 at 2:23 PM, Aditya Raj Bhatt <adityarajbhatt@gmail.com> wrote: > On Wednesday, March 18, 2015 at 1:04:39 PM UTC-5, Laurent Pointal wrote: >> > Can someone also provide a sort of a 'guide' to triple-quoted > comments >> > in general? >> >> A triple ' or " string is a Python string, allowing line-return in > string. > > What do you mean by line-return in string? Is it newline? Does it mean I > can write - > > '''first part > second part''' > > ? > >> If it is in an expression (like a = 5 '''a comment'''), then it must > be a >> valid expression (and here it is not). > > What is not a valid expression here? a = 5 #Valid expression b = c + d # Valid expression a = 5 b = c + d # Invalid expression a = 5 # Valid expression ''' testing comment ''' # Valid expression a = 5 ''' testing comment ''' #Invalid expression Thank you. > >><snip> >> A+ >> Laurent. > > Thank you for the fast and detailed response, > Aditya > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Laurent Pointal <laurent.pointal@laposte.net> |
|---|---|
| Date | 2015-03-18 20:08 +0100 |
| Message-ID | <5509cd1d$0$3354$426a34cc@news.free.fr> |
| In reply to | #87692 |
Aditya Raj Bhatt wrote:
> On Wednesday, March 18, 2015 at 1:04:39 PM UTC-5, Laurent Pointal wrote:
>> > Can someone also provide a sort of a 'guide' to triple-quoted
> comments
>> > in general?
>>
>> A triple ' or " string is a Python string, allowing line-return in
> string.
>
> What do you mean by line-return in string? Is it newline? Does it mean I
> can write -
>
> '''first part
> second part'''
>
> ?
Yes.
>> If it is in an expression (like a = 5 '''a comment'''), then it must
> be a
>> valid expression (and here it is not).
>
> What is not a valid expression here?
What do you expect as result of this combination of an integer with a
string?
>>> a = 5 '''a comment'''
File "<stdin>", line 1
a = 5 '''a comment'''
^
SyntaxError: invalid syntax
You may use an *, and you buid a valid expression with a result
>>> a = 5 * '''a comment'''
But… this may not be the value you wanted for a variable.
>>> a
'a commenta commenta commenta commenta comment'
So, a string is really not a comment, even if triple quote strings can be
used alone to store multi-line "comments".
A+
Laurent.
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2015-03-28 16:34 -0700 |
| Message-ID | <sceehal2svi4ko3pv7thj2sg424a3n1rtm@4ax.com> |
| In reply to | #87692 |
Aditya Raj Bhatt <adityarajbhatt@gmail.com> wrote:
>On Wednesday, March 18, 2015 at 1:04:39 PM UTC-5, Laurent Pointal wrote:
>> > Can someone also provide a sort of a 'guide' to triple-quoted
>> > comments in general?
>>
>> A triple ' or " string is a Python string, allowing line-return in
>> string.
>
>What do you mean by line-return in string? Is it newline? Does it mean I
>can write -
>
>'''first part
>second part'''
>
>?
>
>> If it is in an expression (like a = 5 '''a comment'''), then it must
>> be a
>> valid expression (and here it is not).
>
>What is not a valid expression here?
Were you ever able to extract the Real Answer to your original question
from the froth that resulted? Your basic misunderstanding is that the
triple-quote thing is NOT a comment marker. It is a string literal,
exactly like a single-quoted string, except that it allows embedded
newlines. So, your statement
a = 5 '''a comment'''
is invalid for exactly the same reason that the statement
a = 5 "a comment"
is invalid. Neither one of those statement have any comments.
There is a CONVENTION to embed a literal string as the first line in a
function, to allow for automatic documentation. Whether the literal string
is single-quoted or triple-quoted is irrelevant. That is, these two things
are equivalent:
def func(a):
"This is a function"
return a*2
def func(a):
"""This is a function"""
return a*2
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-18 21:21 +0100 |
| Message-ID | <5230236.iDEbL7tdq2@PointedEars.de> |
| In reply to | #87687 |
Aditya Raj Bhatt wrote: > I always do single line comments with # but just for the sake of it I > tried it with ''' ''' and it gives me a syntax error. > > In both the interpreter, and the source code text file, doing - > > a = 5 '''a comment''' > > results in a syntax error, with the very last quote at the end of the > line highlighted in red. […] > > So can someone tell me why a triple-quoted string gives a syntax error > if only in one line? As with all syntax errors in all programming languages, the code cannot be produced by the grammar (here: <https://docs.python.org/3/reference/grammar.html>, assuming Python 3.x). > Actually, there are other confusions I have too, > regarding using backslashes inside triple-quoted strings to form > multi-line comments, and a general uncertainty about triple-quoted > strings. > > Can someone also provide a sort of a 'guide' to triple-quoted comments > in general? Do not use them as comments in this way. See also my other follow-up. [x] done > Something like how I can just sum up index slices by saying in [a:b], > the 'counting' for a always starts with 0, a is included, everything up > to b but not b is included (assuming this is in fact the correct > explanation ;-)) It is correct: <https://docs.python.org/3/tutorial/introduction.html#strings> <https://docs.python.org/3/tutorial/introduction.html#lists> <https://docs.python.org/3/tutorial/datastructures.html#more-on-lists> <https://docs.python.org/3/library/stdtypes.html#typesseq> -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2015-03-18 13:15 -0500 |
| Message-ID | <mailman.8.1426710961.10327.python-list@python.org> |
| In reply to | #87687 |
On 2015-03-18 10:46, Aditya Raj Bhatt wrote:
> a = 5 '''a comment'''
>
> results in a syntax error
That's to be expected, and happens with any string, not just
triple-quoted:
>>> a = 5 "hello"
> there are no 'true' multiline comments in python and that all those
> 'block' comments are actually triple-quoted strings.
Yes, that's an abuse of strings. It works nicely for doc-strings,
but I avoid it for everything else.
> So can someone tell me why a triple-quoted string gives a syntax
> error if only in one line?
When a string (triple-quoted or otherwise) begins at the beginning of
a line, its return value is ignored at compile-time:
>>> def a():
... print 1
... "test"
... print 2
...
>>> import dis
>>> dis.dis(a)
2 0 LOAD_CONST 1 (1)
3 PRINT_ITEM
4 PRINT_NEWLINE
4 5 LOAD_CONST 2 (2)
8 PRINT_ITEM
9 PRINT_NEWLINE
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
Note that nothing appears in the byte-code for line #3. They Python
interpreter/compiler is smart enough to know that the string can be
discarded.
> Actually, there are other confusions I have too, regarding using
> backslashes inside triple-quoted strings to form multi-line
> comments, and a general uncertainty about triple-quoted strings.
>
> Can someone also provide a sort of a 'guide' to triple-quoted
> comments in general?
Triple-quoted strings aren't particularly magical other than that
they allow you to have multi-line strings and it allows you to have
both single and double quotes inside the string with less mess than
if you have them mixed in a regular string (where you'd need to
escape at least one of them).
s1 = "Single ' and double \" quotes"
s2 = 'Single \' and double " quotes'
s3 = """Single ' and double " quotes"""
If you want to avoid some of the backslash issues, you can make it a
raw triple-quoted string:
s = r"""this is in
c:\temp\new>
for you to test
"""
Note the leading "r" before the quotes. This is what tells Python
not to translate the "\t" and "\n" in the following content.
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2015-03-19 11:06 +1100 |
| Message-ID | <mailman.15.1426723608.10327.python-list@python.org> |
| In reply to | #87687 |
Aditya Raj Bhatt <adityarajbhatt@gmail.com> writes: > I always do single line comments with # but just for the sake of it I > tried it with ''' ''' and it gives me a syntax error. The only comment syntax in Python code is the line-end ‘# …’ syntax. > In both the interpreter, and the source code text file, doing - > > a = 5 '''a comment''' There is no comment on that line. You have an assignment statement immediately followed by a string literal, which is invalid syntax. > http://stackoverflow.com/questions/397148/why-doesnt-python-have-multiline-comments > which says that there are no 'true' multiline comments in python and > that all those 'block' comments are actually triple-quoted strings. That's correct. > So can someone tell me why a triple-quoted string gives a syntax error > if only in one line? Because the string literal – it doesn't matter how it's quoted – can only appear where the syntax allows for a string literal. It isn't a comment, so it can't be freely substituted for a comment. > Can someone also provide a sort of a 'guide' to triple-quoted comments > in general? The guide is simple: There are no triple-quoted comments in Python. If you triple-quote a string literal, it is still a string literal and must follow all the syntax rules for string literals. -- \ “[The RIAA] have the patience to keep stomping. They're playing | `\ whack-a-mole with an infinite supply of tokens.” —kennon, | _o__) http://kuro5hin.org/ | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-03-19 00:53 +0000 |
| Message-ID | <med6mn$ag9$6@dont-email.me> |
| In reply to | #87687 |
On Wed, 18 Mar 2015 10:46:20 -0700, Aditya Raj Bhatt wrote:
> I always do single line comments with # but just for the sake of it I
> tried it with ''' ''' and it gives me a syntax error.
> ...
> So can someone tell me why a triple-quoted string gives a syntax error
> if only in one line?
A triple quoted string is a multiline string literal. A string literal
(of any sort) is a basic python expression.
An expression is often, but by no means exclusively, part of an
assignment statement. However, an expression may also exist just as a
simple expression.
There is nothing special about a triple quoted string that makes it a
comment, other than it is sometimes used as such in it's guise as a basic
expression.
However, you can't have multiple expressions on a line without some sort
of operand or separator between them.
a = 5 '''text'''
is just as wrong as:
q = 4,5,6 [3,5,7,9]
or
k = 6-2 {56:91, 'fred': 'peter'}
or even
m = 62.3 56.7 101.2
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-22 04:14 +0100 |
| Message-ID | <3533816.ZYnZ2OzjCs@PointedEars.de> |
| In reply to | #87712 |
Denis McMahon wrote:
> However, you can't have multiple expressions on a line without some sort
> of operand or separator between them.
String concatenation is implicit in Python, but only with string *literals*:
| $ python
| Python 2.7.9 (default, Mar 1 2015, 12:57:24)
| [GCC 4.9.2] on linux2
| Type "help", "copyright", "credits" or "license" for more information.
| >>> 'foo' 'bar'
| 'foobar'
| $ python3
| Python 3.4.2 (default, Dec 27 2014, 13:16:08)
| [GCC 4.9.2] on linux
| Type "help", "copyright", "credits" or "license" for more information.
| >>> 'foo' 'bar'
| 'foobar'
They do not even have to be on the same line:
| $ python <<'EOT'
| print("foo"
| "bar")
| EOT
| foobar
| $ python3 <<'EOT'
| print("foo"
| "bar")
| EOT
| foobar
(I wished other programming languages had this feature.)
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-03-22 14:36 +1100 |
| Message-ID | <mailman.51.1426995416.10327.python-list@python.org> |
| In reply to | #87810 |
On Sun, Mar 22, 2015 at 2:14 PM, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: > Denis McMahon wrote: > >> However, you can't have multiple expressions on a line without some sort >> of operand or separator between them. > > String concatenation is implicit in Python, but only with string *literals*: Which aren't expressions. Implicit concatenation is part of the syntax, not part of the expression evaluator. https://docs.python.org/2/reference/lexical_analysis.html#string-literal-concatenation https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation > (I wished other programming languages had this feature.) But I absolutely agree. A handful do (Pike, with the same semantics as Python; REXX, but with the proviso that any amount of whitespace between the literals becomes a single space in the resulting string), and it really is a very handy feature. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-22 04:49 +0100 |
| Message-ID | <3971951.908YQu3oQO@PointedEars.de> |
| In reply to | #87811 |
Chris Angelico wrote:
> On Sun, Mar 22, 2015 at 2:14 PM, Thomas 'PointedEars' Lahn
> <PointedEars@web.de> wrote:
>> Denis McMahon wrote:
>>> However, you can't have multiple expressions on a line without some sort
>>> of operand or separator between them.
>> String concatenation is implicit in Python, but only with string
>> *literals*:
>
> Which aren't expressions.
IBTD:
,-<https://docs.python.org/3/reference/grammar.html>
|
| […]
| expr: xor_expr ('|' xor_expr)*
| xor_expr: and_expr ('^' and_expr)*
| and_expr: shift_expr ('&' shift_expr)*
| shift_expr: arith_expr (('<<'|'>>') arith_expr)*
| arith_expr: term (('+'|'-') term)*
| term: factor (('*'|'/'|'%'|'//') factor)*
| factor: ('+'|'-'|'~') factor | power
| power: atom trailer* ['**' factor]
| atom: ('(' [yield_expr|testlist_comp] ')' |
| '[' [testlist_comp] ']' |
| '{' [dictorsetmaker] '}' |
| NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
> Implicit concatenation is part of the syntax, not part of the expression
> evaluator.
Reads like nonsense to me.
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-03-22 15:11 +1100 |
| Message-ID | <mailman.52.1426997516.10327.python-list@python.org> |
| In reply to | #87812 |
On Sun, Mar 22, 2015 at 2:49 PM, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:
>> Implicit concatenation is part of the syntax, not part of the expression
>> evaluator.
>
> Reads like nonsense to me.
What do you mean? String concatenation by abuttal is as much a
syntactic element as the distinction between regular, raw, and
triple-quoted string literals. By the time you get to AST (never mind
about byte code), that information is gone:
>>> print(ast.dump(ast.parse("""
... x = '''hello'''
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])
>>> print(ast.dump(ast.parse("""
... x = r'hello'
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])
>>> print(ast.dump(ast.parse("""
... x = "hello"
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])
>>> print(ast.dump(ast.parse("""
... x = "he" "ll" "o"
... """)))
Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Str(s='hello'))])
Nothing in the expression evaluator knows or cares about what kind of
string literal you used, nor whether you included more than one. It's
all just alternative forms of string literal.
ChrisA
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web