Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; '"""': 0.07; 'abuse': 0.07; 'ignored': 0.07; 'nicely': 0.07; 'string': 0.09; '"r"': 0.09; 'escape': 0.09; 'mess': 0.09; 'mixed': 0.09; 'otherwise)': 0.09; 'strings.': 0.09; 'subject:Why': 0.09; 'translate': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '"test"': 0.16; '-tkc': 0.16; 'a():': 0.16; 'backslash': 0.16; 'backslashes': 0.16; 'bhatt': 0.16; 'discarded.': 0.16; 'expected,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'string:': 0.16; 'subject: \n ': 0.16; 'subject:comment': 0.16; 'subject:quoted': 0.16; 'subject:simple': 0.16; 'subject:triple': 0.16; 'wrote:': 0.18; '(where': 0.19; '>>>': 0.22; 'appears': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; "aren't": 0.24; 'string,': 0.24; 'tells': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'sort': 0.25; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'gives': 0.31; 'comments': 0.31; 'comments,': 0.31; 'multiline': 0.31; 'quotes': 0.31; 'allows': 0.31; 'regular': 0.32; 'beginning': 0.33; 'raw': 0.33; '(2)': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'received:10': 0.37; 'enough': 0.39; 'tell': 0.60; 'issues,': 0.61; 'provide': 0.64; 'to:addr:gmail.com': 0.65; 'subject:. ': 0.67; 'line,': 0.68; 'results': 0.69; "'true'": 0.84; 'actually,': 0.84; 'magical': 0.91 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1426702455020:107552668 X-MC-Ingress-Time: 1426702455020 Date: Wed, 18 Mar 2015 13:15:28 -0500 From: Tim Chase To: Aditya Raj Bhatt Cc: python-list@python.org Subject: Re: A simple single line, triple-quoted comment is giving syntax error. Why? In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426710961 news.xs4all.nl 2889 [2001:888:2000:d::a6]:44602 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87700 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