Wrapping long code on a new line in Python
Intro | |
Example | |
Wrapping f-string | |
Assignment | |
Function | |
if | |
List | |
Wrapping import | |
Related Articles |
Introductio
When line exceeds 80 symbols according to PEP 8 it should be wrapped.
Example
Example of a string that is too long
url = your_base_url + "/monitor-service/api/v1/components/744618a0-78c5-4e19-78f4-6d215bde64a5"
To make a line break, use the \ symbol
url = your_base_url + \
"/monitor-service/api/v1/components/744618a0-78c5-4e19-78f4-6d215bde64a5"
Or
url = your_base_url + "/monitor-service/api/v1/components/" \
"744618a0-78c5-4e19-78f4-6d215bde64a5"
f-string
When wrapping f-string , for example:
print(f'\n\nPOST to {your_url} response status code is {response.status_code}\n')
New line should also starts with f
print(f'\n\nPOST to {your_url} response status code is '
f'{response.status_code}\n')
If after the wrapping some line doesn't have variable placeholders - you don't need to write f
url: str = "https://aredel.com" print("Website about software testing:" f"{url} has https," "can be found at link.")
If a long f-string is used when declaring a variable, it must be enclosed in brackets for wrapping
current_time = time.strftime("%Y-%m-%d-%H-%M-%S") epoch_time = int(time.time()) url = "https://testsetup.ru" message = (f"В данный момент: {current_time}" f"linux epoch time: {epoch_time}" f"{url} содержит https, переходите по ссылке")
Assignment
When wrapping expressiong like a = b, where b is being too long:
# Good: # Wrapping by opening bracket. foo = long_function_name(var_one, var_two, var_three, var_four) # Alternative is a "Handing" indent. foo = long_function_name( var_one, var_two, var_three, var_four) # If you put a comma at the end, the closing bracket # can be placed under the first non-empty character. result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', ) # Or to the beginning of the line. result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', ) # Wrong: # It is forbidden to list arguments on the first line # if the next line is not aligned. foo = long_function_name(var_one, var_two, var_three, var_four)
Function Declaration
If you need to declare a function with a large number of parameters:
# Right: # You need to indent 4 spaces to highlight the parameters. def long_function_name( var_one, var_two, var_three, var_four): print(var_one) # Wrong # Parameters are not highlighted and difficult to read def long_function_name( var_one, var_two, var_three, var_four): print(var_one)
if
Branching based on if is allowed in the following ways::
# No indentation. if (this_is_one_thing and that_is_another_thing): do_something() # A good trick is to add a comment, which will # improve readability in editors with syntax highlighting. if (this_is_one_thing and that_is_another_thing): # Since both conditions are true, we can frobnicate. do_something() # Allowed to add indentation before and. if (this_is_one_thing and that_is_another_thing): do_something()
Declaration of lists
Lists can be declared in two ways:
my_list = [ 1, 2, 3, 4, 5, 6, ] my_list = [ 1, 2, 3, 4, 5, 6, ]
Wrapping import
If you need to do a transfer when importing modules, you need to enclose the module names in brackets
from devhopspac import (first_long_function_name, second_long_function_name)