PEP 750 – टेम्पलेट स्ट्रिंग्स (t-strings) को मंजूरी
(peps.python.org)- PEP 750 Python में एक नया string literal टेम्पलेट स्ट्रिंग्स(
t"...") पेश करता है - यह f-string का generalized रूप है, जो
Templatetype बनाकर string और interpolated values को जोड़ने से पहले प्रोसेस करने की सुविधा देता है - इसे web templates, security checks, और DSL(Domain-Specific Language) जैसे उपयोगों में काम में लिया जा सकता है
दूसरे PEPs के साथ संबंध
- f-string को PEP 498 में पेश किया गया था, और PEP 701 में उसकी syntax का विस्तार किया गया
- PEP 501 ने generalized template strings(
i-string) का प्रस्ताव दिया था, लेकिन उसे स्थगित कर दिया गया - मौजूदा PEP 750, PEP 501 का सरल और generalized रूप है, और पहले के विचारों को आगे बढ़ाता है
प्रेरणा और आवश्यकता
- f-string सरल है, लेकिन interpolated values को पहले से प्रोसेस नहीं कर सकता, जिससे security समस्याएँ पैदा हो सकती हैं
- SQL injection, XSS attacks जैसी security vulnerabilities का खतरा रहता है
- टेम्पलेट स्ट्रिंग्स का उपयोग करने पर interpolated values को पहले प्रोसेस करके सुरक्षित रूप से इस्तेमाल किया जा सकता है
उदाहरण:
evil = "<script>alert('evil')</script>"template = t"<p>{evil}</p>"assert html(template) == "<p><script>alert('evil')</script></p>"
टेम्पलेट स्ट्रिंग्स की specification
टेम्पलेट string literal
- इसे
tयाTprefix के साथ परिभाषित किया जाता है - यह
string.templatelib.Templatetype के रूप में evaluate होता है - f-string जैसी syntax का समर्थन करता है, और nesting भी संभव है
rprefix के साथ जोड़ा जा सकता है (rt,tr)u,bprefix के साथ नहीं जोड़ा जा सकता- f-string और टेम्पलेट स्ट्रिंग्स को मिलाकर इस्तेमाल नहीं किया जा सकता
Template type
- यह immutable type है, और इसमें ये attributes होते हैं:
strings: string fragments का tupleinterpolations: interpolated value objects का tuplevalues: interpolated values का value tuple__iter__(): strings और interpolated values को क्रम से लौटाने वाला iterator
Interpolation type
value: evaluated resultexpression: मूल interpolated expression stringconversion: conversion method (r,s,aया None)format_spec: format string
उदाहरण:
name = "World"template = t"Hello {name!r}"assert template.interpolations[0].conversion == "r"
debug specifier =
t"{value=}"कोt"value={value!r}"की तरह समझा जाता है- spaces भी वैसे ही सुरक्षित रहते हैं (
t"{value = }"→"value = {value!r}")
टेम्पलेट स्ट्रिंग concatenation
+operator सेTemplateऔरstr, याTemplateऔरTemplateको जोड़ा जा सकता है- concatenation का result हमेशा
Templatetype होता है - implicit string concatenation (
t"Hello " t"World") भी संभव है
टेम्पलेट स्ट्रिंग्स को प्रोसेस करने के तरीके
उदाहरण: uppercase processing function
def lower_upper(template):parts = []for s in template:if isinstance(s, str): parts.append(s.lower())else: parts.append(str(s.value).upper())
return "".join(parts)
उदाहरण: f-string जैसा ही processing implementation
f()function के जरिए f-string जैसा ही result बनाया जा सकता है
उदाहरण: structured logging
- टेम्पलेट स्ट्रिंग्स का उपयोग करने पर log message और structured values को एक साथ output किया जा सकता है
- इसे
StructuredMessageयाlogging.Formattersubclass के रूप में implement किया जा सकता है
उदाहरण: HTML template processing
html()function interpolation की position के अनुसार content को सही तरह escape करता है या attribute के रूप में handle करता है- nested templates का भी समर्थन है
उन्नत उपयोग पैटर्न
- structural pattern matching (
matchstatement) के उपयोग की सिफारिश की गई है - static strings को cache keys के रूप में इस्तेमाल करके efficient memoization संभव है
- AST जैसी intermediate representation में parse करके प्रोसेस किया जा सकता है
- Lazy या Async evaluation के लिए
lambda,awaitका उपयोग किया जा सकता है
टेम्पलेट स्ट्रिंग्स और मौजूदा format strings का संबंध
- मौजूदा
.format()जैसी शैली में template functions को परिभाषित किया जा सकता है - बाहरी strings को parse करके उन्हें
Templateमें बदलने वालाfrom_format()भी संभव है
compatibility, security, learning
- Python के पुराने versions में syntax error हो सकता है
- security के लिहाज से template processing सुरक्षा बढ़ाती है
- f-string जैसी syntax होने से इसे सीखना आसान है
नया template approach क्यों?
- Jinja जैसे मौजूदा templates आमतौर पर user customization या designers के लिए होते हैं
- developers सीधे templates को संभाल सकें, इसके लिए Python language level पर support की जरूरत है
- इससे expressiveness और type checking जैसे फायदे लिए जा सकते हैं
उदाहरण पैटर्न सारांश
- structural pattern matching और sub-attribute matching
- templates को function की तरह reuse करना
- nested templates का समर्थन
- Lazy/Async evaluation का समर्थन
- static/dynamic separation से cache optimization
अन्य design considerations
- templates को string में convert नहीं किया जाता, और
__str__()implement नहीं है - संबंधित classes
string.templatelibmodule में दी गई हैं Template,Interpolationकी तुलना object identity के आधार पर की जाती है==या<operator का समर्थन नहीं है
reference implementation और examples
- CPython implementation
- उदाहरण और परीक्षण प्रदान किए गए हैं
अस्वीकृत विचार
- मनमाना prefix उपयोग (
my_tag"...") - सभी interpolated expressions की lazy evaluation
- protocol के रूप में implementation
__eq__,__hash__का पुनर्परिभाषण- मूल string की पूरी पुनर्स्थापना
Decodedtype जोड़ना- binary template strings का समर्थन
- format type (
"html","sql"आदि) बताने की सुविधा - string concatenation पर प्रतिबंध
- arbitrary converter (
!x) की अनुमति
3 टिप्पणियां
सबसे संतोषजनक formatting तो बस JS और Python में ही है। दूसरी भाषाएँ तो थोड़ी...
स्पष्ट, और संभव हो तो ऐसा करने का केवल एक ही स्पष्ट तरीका होना चाहिए। (There should be one-- and preferably only one --obvious way to do it.)
Hacker News राय
अलग-अलग भाषाएँ string formatting को जिस तरह संभालती हैं, वह दिलचस्प है
Nick Humrich, जिन्होंने PEP 501 को फिर से लिखकर t-strings पेश किए, इस PEP के स्वीकार होने से बहुत खुश हैं
यह निश्चित नहीं है कि language-level feature वास्तव में मूल्यवान है या नहीं
f-strings पसंद हैं, लेकिन उनमें evaluation को defer नहीं किया जा सकता, यह समस्या है
lit-html के maintainer के रूप में, JavaScript के tagged template literals से इसकी समानता दिलचस्प लगती है
उम्मीद है कि JavaScript के tagged template literals की तरह Python में भी यह HTML auto-escaping या SQL parameterization में मदद करेगा
कुछ लोगों का मानना है कि Python, PHP जैसा बनता जा रहा है
भाषा में लगातार नई चीज़ें जोड़ने को लेकर असंतोष है
एक राय यह भी है कि यह PEP, C++ के P1819 जैसा है
कुछ लोगों का कहना है कि PEP का code बहुत verbose है