Skip to content

Level 2#2

Open
mishakov1c wants to merge 4 commits intomainfrom
level_2
Open

Level 2#2
mishakov1c wants to merge 4 commits intomainfrom
level_2

Conversation

@mishakov1c
Copy link
Owner

No description provided.

# код писать тут
product_type = request.GET.get('type')
if product_type:
products = [product for product in PRODUCTS if product['type'] == product_type]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я предпочитаю не создавать переменные в ветках ифа – одно неверное движение и ты используешь неопределённую переменную. Вместо этого я её создаю перед ифом с подходящим значением по-умолчанию. Тут правильное значение по-умолчанию ещё и код может упростить слегка.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут у тебя в разных ветках ифа разный статус, а всё остальное одинаковое. От этой копипасты бы избавиться.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Оборачивание ключей в лист приводит к копированию ключей в отдельный список в памяти. Это не только занимает чуть больше памяти каждый запрос, но ещё и медленнее работает: проверка элемента на вхождение в словаре быстрее чем в списке.

Советую избавиться от этой переменной.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот для таких случаев, когда ты создаёшь переменную, а потом сразу применяешь её пригождается валрус оператор:

if product_type := request.GET.get('type'):
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments