Simple question:

 

When lines get long, what points does splitting them make sense and what
methods are preferred?

 

Details.

 

I am used to many languages where you can continue a statement across
multiple lines. What they share in common is the fact they do not use
indenting for the use Python makes of it. They often use something like
"{.}" to combine multiple statements into a single body and carriage returns
are generally ignored as multiple lines are parsed to search for thee end of
the compound statement at the "}" . In many languages, parentheses and
square brackets and even angle brackets can serve a similar function. Some
languages that insist on ending a statement with ";" are even looser. Many
treat a trailing comma or plus sign (and other symbols) as a hint to
continue reading another line.

 

So I would like some feedback on ways to deal with longer statements in
Python.

 

Yes, I am aware of ways to break up something long by breaking in into
multiple statements. It feels silly to do this:

 

A = "long string"

B = "another string"

.

Z = "last string"

 

When you meant to write one long string in the first place or connect a
bunch of them with adjacency or a "+"

 

By breaking them into snippets like above, and using short variable names,
you can combine them like this:

 

Combined = A + B + . + Z

 

Or even worse:

 

Combined = ""

Combined += A

Combined += B

.

Combined += Z

 

The above was ONE example of many where I bend my code in ways to make the
lines reasonably short so reading and editing are easy.

 

There are places you can break lines as in a comprehension such as this set
comprehension:

 

    letter_set = { letter

                   for word in (left_list + right_list)

                   for letter in word }

 

The above is an example where I know I can break because the {} is holding
it together. I know I can break at each "for" or "if" but can I break at
random places? Here is a bizarre example that worked:

 

>>> [ x *

      y

      for x in

      range(1,5,2)

      for y

      in range(2,

                       6

                       ,2)

      if x + y <

      9

      ]

[2, 4, 6, 12]

 

So clearly when you use [.] it ignores indentation until it gets to the end.
I see that parentheses sometimes offer a similar protection as in the new
print function:

 

>>> print(1,

                  2

                  ,

                  3)

1 2 3

 

But python has a few features like allowing a comma at the end of a tuple
that get in the way:

 

>>> x,y = 1,

Traceback (most recent call last):

  File "<pyshell#15>", line 1, in <module>

    x,y = 1,

ValueError: not enough values to unpack (expected 2, got 1)

 

DUH! I was going to put the rest on the next line!

 

So I added parentheses and that does the trick here.

 

>>> x,y = (1,

                   2

                   )

 

Similarly, it is a syntax error if I simply end with something like a +

 

>>> x = 1 +

SyntaxError: invalid syntax

>>> x = (1 +

                2)

>>> x

3

 

I AM NOT SAYING THIS IS HORRIBLE, just asking what pythonic ways are best to
use to get around things.

 

One that seems not to be favored is a backslash at the end of a line:

 

>>> x = 1 + \

                2 + \

                3

>>> x

6

 

Although my examples use short numbers, remember I might be using this with
long lines and not want to mess up the overall indentation. Strings have a
similar issue:

 

>>> x = "hello

SyntaxError: EOL while scanning string literal

>>> x = """hello

world"""

>>> x

'hello\nworld'

>>> x = "hello \

                world!"

>>> x

'hello \tworld!''

 

As shown, the triple quotes sort of work but introduce and keep carriage
returns and formatting inside. The backslash at the end suppressed the
former but keeps the latter. Yes, there are routines you can pass the string
through afterward to remove leading whitespace or carriage returns.

 

I will stop here with saying that unlike many languages, parentheses must be
used with care in python as they may create a tuple or even generator
expression. Curly braces may make a set or dictionary. Options for splitting
the code without messing up indenting cues would be nice to understand.

 

 

 

 

 

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to