PYPROGLIB Telegram 7084
🔥 Новое в Python 3.14: functools.Placeholder

functools.partial давно помогает замораживать аргументы функций. Но раньше были ограничения: если аргументы были только позиционные, их нельзя было зафиксировать через partial с именованными параметрами.

В Python 3.14 это решает новый functools.Placeholder.

➡️ Пример: удаление пунктуации

До 3.14 приходилось писать обёртку:
import string

_table = str.maketrans("", "", string.punctuation)

def remove_punctuation(s):
return s.translate(_table)

print(remove_punctuation("Hello, world!")) # Hello world


С Placeholder можно напрямую зафиксировать второй аргумент:
from functools import partial, Placeholder
import string

_table = str.maketrans("", "", string.punctuation)
remove_punctuation = partial(str.translate, Placeholder, _table)

print(remove_punctuation("Hello, world!")) # Hello world


Что нужно понимать:
📝 Placeholder резервирует место для аргумента, который придёт позже.
📝 Вызов remove_punctuation("text") превращается в str.translate("text", _table).
📝 Удобнее импортировать короче:
  from functools import Placeholder as _P


Теперь partial можно использовать и с функциями, где аргументы позиционные-only.
Это открывает дорогу к более лаконичному и гибкому коду, без лишних обёрток.

🐸 Библиотека питониста

#буст
Please open Telegram to view this post
VIEW IN TELEGRAM
👍98🤔3🔥2



tgoop.com/pyproglib/7084
Create:
Last Update:

🔥 Новое в Python 3.14: functools.Placeholder

functools.partial давно помогает замораживать аргументы функций. Но раньше были ограничения: если аргументы были только позиционные, их нельзя было зафиксировать через partial с именованными параметрами.

В Python 3.14 это решает новый functools.Placeholder.

➡️ Пример: удаление пунктуации

До 3.14 приходилось писать обёртку:

import string

_table = str.maketrans("", "", string.punctuation)

def remove_punctuation(s):
return s.translate(_table)

print(remove_punctuation("Hello, world!")) # Hello world


С Placeholder можно напрямую зафиксировать второй аргумент:
from functools import partial, Placeholder
import string

_table = str.maketrans("", "", string.punctuation)
remove_punctuation = partial(str.translate, Placeholder, _table)

print(remove_punctuation("Hello, world!")) # Hello world


Что нужно понимать:
📝 Placeholder резервирует место для аргумента, который придёт позже.
📝 Вызов remove_punctuation("text") превращается в str.translate("text", _table).
📝 Удобнее импортировать короче:
  from functools import Placeholder as _P


Теперь partial можно использовать и с функциями, где аргументы позиционные-only.
Это открывает дорогу к более лаконичному и гибкому коду, без лишних обёрток.

🐸 Библиотека питониста

#буст

BY Библиотека питониста | Python, Django, Flask




Share with your friend now:
tgoop.com/pyproglib/7084

View MORE
Open in Telegram


Telegram News

Date: |

Hui said the time period and nature of some offences “overlapped” and thus their prison terms could be served concurrently. The judge ordered Ng to be jailed for a total of six years and six months. A few years ago, you had to use a special bot to run a poll on Telegram. Now you can easily do that yourself in two clicks. Hit the Menu icon and select “Create Poll.” Write your question and add up to 10 options. Running polls is a powerful strategy for getting feedback from your audience. If you’re considering the possibility of modifying your channel in any way, be sure to ask your subscribers’ opinions first. Hashtags are a fast way to find the correct information on social media. To put your content out there, be sure to add hashtags to each post. We have two intelligent tips to give you: A vandalised bank during the 2019 protest. File photo: May James/HKFP. 6How to manage your Telegram channel?
from us


Telegram Библиотека питониста | Python, Django, Flask
FROM American