PYTHON_JOB_INTERVIEW Telegram 1156
🐍 Совет дня для опытных Python-разработчиков

📌 Используй декораторы с параметрами — мощный приём для логирования, контроля, кэширования и кастомных проверок.

Пример: логгер, у которого можно задать уровень логирования через аргумент:


import functools
import logging

def log(level=logging.INFO):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
logging.log(level, f"Вызов {func.__name__} с args={args}, kwargs={kwargs}")
return func(*args, **kwargs)
return wrapper
return decorator

@log(logging.DEBUG)
def compute(x, y):
return x + y


Зачем это нужно:

Декоратор гибко настраивается;

Подходит для трассировки в проде и отладки в деве;

Сохраняет сигнатуру и docstring благодаря @functools.wraps.

⚠️ Совет: избегай вложенности >2 уровней и всегда пиши тесты на поведение декоратора.

Python даёт инструменты, которые выглядят магией, но работают стабильно — если знаешь, как ими пользоваться.
🔥7👍32🤔1



tgoop.com/python_job_interview/1156
Create:
Last Update:

🐍 Совет дня для опытных Python-разработчиков

📌 Используй декораторы с параметрами — мощный приём для логирования, контроля, кэширования и кастомных проверок.

Пример: логгер, у которого можно задать уровень логирования через аргумент:


import functools
import logging

def log(level=logging.INFO):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
logging.log(level, f"Вызов {func.__name__} с args={args}, kwargs={kwargs}")
return func(*args, **kwargs)
return wrapper
return decorator

@log(logging.DEBUG)
def compute(x, y):
return x + y


Зачем это нужно:

Декоратор гибко настраивается;

Подходит для трассировки в проде и отладки в деве;

Сохраняет сигнатуру и docstring благодаря @functools.wraps.

⚠️ Совет: избегай вложенности >2 уровней и всегда пиши тесты на поведение декоратора.

Python даёт инструменты, которые выглядят магией, но работают стабильно — если знаешь, как ими пользоваться.

BY Python вопросы с собеседований


Share with your friend now:
tgoop.com/python_job_interview/1156

View MORE
Open in Telegram


Telegram News

Date: |

During a meeting with the president of the Supreme Electoral Court (TSE) on June 6, Telegram's Vice President Ilya Perekopsky announced the initiatives. According to the executive, Brazil is the first country in the world where Telegram is introducing the features, which could be expanded to other countries facing threats to democracy through the dissemination of false content. 1What is Telegram Channels? Read now Telegram message that reads: "Bear Market Screaming Therapy Group. You are only allowed to send screaming voice notes. Everything else = BAN. Text pics, videos, stickers, gif = BAN. Anything other than screaming = BAN. You think you are smart = BAN. It’s yet another bloodbath on Satoshi Street. As of press time, Bitcoin (BTC) and the broader cryptocurrency market have corrected another 10 percent amid a massive sell-off. Ethereum (EHT) is down a staggering 15 percent moving close to $1,000, down more than 42 percent on the weekly chart.
from us


Telegram Python вопросы с собеседований
FROM American