Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix certain 'Line too long' errors in a Python file?

Tags:

python

flake8

I have the following line inside a for loop (i.e. it's indented by 4 spaces):

    abcdefgh_ijklm_nopqrstuvwxy = abcdefgh_ijklm_nopqrstuvwxy.append(abc_de)

The line is 80 characters long. How can I split it up so that it I do not get a 'Line too long' notification? Please note that I've changed the variable names for privacy reasons (not my code), but I can't modify the name of the variable, so naming it something shorter to fix the problem is not a viable option

As a secondary question, how would I split up a formatted string of the form:

data_header = f"FILE_{heading_angles}_{moment_of_inertia}_{mass_of_object}_{type_of_object}"

to span multiple lines? I already tried

data_header = f"FILE_{heading_angles}_{moment_of_inertia}_"
              f"{mass_of_object}_{type_of_object}"

but that gives me an indentation error.

Any kind of help would be greatly appreciated!

like image 312
Kadhir Avatar asked Jul 02 '26 06:07

Kadhir


1 Answers

Hope that these points answer your questions:

  1. To simplify your expressions, try to replace the variables with simpler ones before the expressions. This may be inappropriate, if more serious operations are needed. For example:
a = abcdefgh_ijklm_nopqrstuvwxy
b = abcdefgh_ijklm_nopqrstuvwxy.append(abc_de)
a = b
  1. In your case, try using a forward-leaning backlash (\) at the end of the line. For example:
if a == True and \
   b == False

Here is a link from another discussion on a similar matter.

Hope this helps.

like image 122
Ricards Agapovs Avatar answered Jul 04 '26 19:07

Ricards Agapovs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!