Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > line continuation for lines ending in "and" or "or"

Reply
Thread Tools

line continuation for lines ending in "and" or "or"

 
 
Russ P.
Guest
Posts: n/a
 
      06-05-2008
I've always appreciated Python's lack of requirement for a semi-colon
at the end of each line. I also appreciate its rules for automatic
line continuation. If a statement ends with a "+", for example, Python
recognizes that the statement obviously must continue.

I've noticed, however, that the same rule does not apply when a line
ends with "and," "or," or "not." Yes, it's a minor point, but
shouldn't the same rule apply?

Seems like it would be easy to add.
 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      06-05-2008
On Jun 4, 10:09*pm, "Russ P." <Russ.Paie...@gmail.com> wrote:
> I've always appreciated Python's lack of requirement for a semi-colon
> at the end of each line. I also appreciate its rules for automatic
> line continuation. If a statement ends with a "+", for example, Python
> recognizes that the statement obviously must continue.
>
> I've noticed, however, that the same rule does not apply when a line
> ends with "and," "or," or "not." Yes, it's a minor point, but
> shouldn't the same rule apply?
>
> Seems like it would be easy to add.


Huh? This doesn't work either:

>>> x = 2 +

File "<stdin>", line 1
x = 2 +
^
SyntaxError: invalid syntax

Implicit line continuation only happens if you have an unmatched '('.

>>> x = (2 +

... 2
... )
>>> x

4
 
Reply With Quote
 
 
 
 
Russ P.
Guest
Posts: n/a
 
      06-05-2008
On Jun 4, 9:01 pm, Dan Bishop <danb...@yahoo.com> wrote:
> On Jun 4, 10:09 pm, "Russ P." <Russ.Paie...@gmail.com> wrote:
>
> > I've always appreciated Python's lack of requirement for a semi-colon
> > at the end of each line. I also appreciate its rules for automatic
> > line continuation. If a statement ends with a "+", for example, Python
> > recognizes that the statement obviously must continue.

>
> > I've noticed, however, that the same rule does not apply when a line
> > ends with "and," "or," or "not." Yes, it's a minor point, but
> > shouldn't the same rule apply?

>
> > Seems like it would be easy to add.

>
> Huh? This doesn't work either:
>
> >>> x = 2 +

>
> File "<stdin>", line 1
> x = 2 +
> ^
> SyntaxError: invalid syntax
>
> Implicit line continuation only happens if you have an unmatched '('.
>
> >>> x = (2 +

>
> ... 2
> ... )>>> x
>
> 4


Darnit! You're right. I've been reading up on Scala lately, and I
guess I got confused. Well, it wouldn't be a bad idea for Python to do
what I thought it did, *plus* what I said it ought to do.

Scala is a nice language, by the way. Check it out when you get a
chance (http://www.scala-lang.org). I'm thinking about switching over
to it from Python if I can. I just wish it had default arguments and
argument passing by keyword. Now, those are a couple of features that
I really appreciate in Python. Oh, and I wish Scala used "and" and
"or" rather than "&&" and "||". There's another thing Python got right.
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      06-05-2008
Dan Bishop <> wrote:
>On Jun 4, 10:09*pm, "Russ P." <Russ.Paie...@gmail.com> wrote:
>> I've always appreciated Python's lack of requirement for a semi-colon
>> at the end of each line. I also appreciate its rules for automatic
>> line continuation. If a statement ends with a "+", for example, Python
>> recognizes that the statement obviously must continue.
>>
>> I've noticed, however, that the same rule does not apply when a line
>> ends with "and," "or," or "not." Yes, it's a minor point, but
>> shouldn't the same rule apply?
>>
>> Seems like it would be easy to add.

>...
>Implicit line continuation only happens if you have an unmatched '('.
>
>>>> x = (2 +

>... 2
>... )
>>>> x

>4


.... or an unmatched [ or an unmatched {.
--
Tim Roberts,
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      06-05-2008

"Russ P." <> wrote in message
news:0d723e6d-f81b-4545-b406-...
|. Well, it wouldn't be a bad idea for Python to do
| what I thought it did, *plus* what I said it ought to do.

A line ending in an operator is ambiguous in that it *could* indicate that
the programmer intends to continue on the next line while it also could
indicate that the programmer forgot to finish before hitting return, or
that something got erased but not replaced. Moreover, the second
possibility is actual (it actually happens) and not just theoretical.
Moreover, the next line realistically could 'complete' the incomplete line
'by accident', so that the syntax bug would not get flagged.

In such situations, some might lean toward the plausible guess choice, but
Guido leans in the direction of choosing the bug interpretation. So he
included the '\' mechanism. It is already used in strings to mean "do not
take the next char literally", so having it mean "do not take the following
end-of-line literally" is only a tiny step.

Terry Jan Reedy





 
Reply With Quote
 
Paul Boddie
Guest
Posts: n/a
 
      06-05-2008
On 5 Jun, 22:40, "Terry Reedy" <tjre...@udel.edu> wrote:
>
> A line ending in an operator is ambiguous in that it *could* indicate that
> the programmer intends to continue on the next line while it also could
> indicate that the programmer forgot to finish before hitting return, or
> that something got erased but not replaced.


Yes, this is an excellent point. For the logical operators, consider
code like the following...

x = obj1.something() and obj2.conditional() and # time for lunch!
obj4.obligatory_something()

Although a trailing "and" or "or" operator might suggest a
continuation of the expression on the next line, one has to consider
whether the next line (or almost any line defining an expression)
should suggest possible membership of an expression on the preceding
line by default for its contents. One could, of course, insist on
indentation to prevent such ambiguity since the second line above
shouldn't be indented further if part of a separate statement. More
work is definitely needed on such a proposal, certainly.

Paul
 
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
Re: maximum continuation lines in C++ Fred Zwarts C++ 1 05-28-2010 09:15 AM
Re: maximum continuation lines in C++ AnonMail2005@gmail.com C++ 6 05-27-2010 11:42 PM
Re: maximum continuation lines in C++ Alf P. Steinbach C++ 0 05-27-2010 09:11 PM
Questions about input lines (maximum length and continuation) Robert Dodier Python 1 07-04-2007 03:46 AM
How to determine if a line of python code is a continuation of the line above it Sandra-24 Python 5 04-09-2006 08:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57