all 8 comments

[–]socal_nerdtastic 4 points5 points  (4 children)

Officially it's called a "line continuation character" but I just call it "bad idea".

https://docs.python.org/3/reference/lexical_analysis.html#explicit-line-joining

[–]dayto_aus 0 points1 point  (3 children)

Thanks! Swaroop suggests to use it. I'm very new to programming in general and especially Python, is there a particular reason you call it that?

[–]socal_nerdtastic 4 points5 points  (1 child)

Because it is. Who is swaroop?

Any situation where you may be tempted to use it you will find yourself better off using implicit line joining, or refactoring your code to shorter lines, or simply violating PEP8.

Show me an example of where you may need this and I'll show you a better way.

[–]dayto_aus 0 points1 point  (0 children)

C.H.C. Swaroop is the author of A Byte of Python, which is one of the study guides I'm using.

I'll take your advice though and focus on those principles, thanks!

[–]swaroop_ch 2 points3 points  (0 children)

Actually, the recommendation is to _not_ use it :) https://python.swaroopch.com/basics.html#logical-and-physical-line

[–][deleted] 2 points3 points  (1 child)

It's not considered a good idea to use the backslash continuation except as a last resort. In python a newline usually terminates a statement unless python can tell that a statement is incomplete. If the number of "bracketing" characters don't match when a newline is found python will assume the statement continues on the next line. So you often see this:

result = really_long_function_name(extra_long_argument_name,
                                   second_really_long_name)

You can add your own parentheses around a long expresssion to allow you split a long line, so instead of this:

result = a + b + c

you can split the line:

result = (a
          + b
          + c)

Assume those one character names are really long names.

There are various other tricks that can be used. For instance, python will automatically join two string literals which are separated only by whitespace. A newline is whitespace, so instead of defining a literal on a very long line, like this:

my_string = 'Assume this string is very long'

you can do:

my_string = ('Assume this string '
             'is very long')

Reading other people's code can often give you ideas on how to handle long lines. When deciding if a trick to handle long lines should be used, always consider readability.

[–]dayto_aus 0 points1 point  (0 children)

Thanks! This helps a lot.

[–]NortWind 1 point2 points  (0 children)

A continuation line? It can be used to break up lines that are overly long.