Wrapping long code on a new line in Python

Contents
Intro
Example
Wrapping f-string
Assignment
Funtion
if
List
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')

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, ]

Related Articles
Python
Интерактивный режим
str: строки
\: перенос строки
Списки []
if, elif, else
Циклы
Методы
Функции
*args **kwargs
enum
Опеределить тип переменной Python
Тестирование с помощью Python
Работа с REST API на Python
Файлы: записать, прочитать, дописать, контекстный менеджер…
Скачать файл по сети
SQLite3: работа с БД
datetime: Date and Time в Python
json.dumps
Selenium + Python
Сложности при работе с Python
DJANGO
Flask
Скрипт для ZPL принтера
socket :Python Sockets
Виртуальное окружение
subprocess: выполнение bash команд из Python
multiprocessing: несколько процессов одновременно
psutil: cистемные ресурсы
sys.argv: аргументы командной строки
PyCharm: IDE
pydantic: валидация данных
paramiko: SSH из Python
enumerate
f-string

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedbak and Questions in Telegram

@aofeedchat