Python के नए `t-strings`
(davepeck.org)- t-strings Python 3.14 में नया जोड़ा गया सुरक्षित और लचीला string processing फीचर है
- मौजूदा f-string से अलग, t-string string नहीं बल्कि
Templateobject लौटाता है, जिससे ऑटोमैटिक output के बिना सुरक्षित processing संभव होती है - t-string में HTML, SQL जैसी dynamic input को सुरक्षित रूप से escape करने की संरचना होती है
- यह JavaScript के tagged templates जैसा कॉन्सेप्ट है, इसलिए कई तरह के transformation और processing extension संभव हैं
- अगर Python developer tools ecosystem इस फीचर को अच्छे से support करे, तो web/security-केंद्रित string processing में बड़ा बदलाव आ सकता है
Python का नया फीचर: t-strings (Template Strings)
- Python 3.14 से
t"..."syntax में इस्तेमाल होने वाले Template strings (t-strings) को आधिकारिक फीचर के रूप में जोड़ा गया है - मौजूदा f-string से अलग, t-string तुरंत string में evaluate नहीं होता, बल्कि
string.templatelib.Templateobject के रूप में evaluate होता है - इस object को output से पहले अलग processing step की ज़रूरत होती है, और इसी प्रक्रिया के जरिए dynamic values की सुरक्षित handling और transformation संभव होती है
f-string जोखिमपूर्ण क्यों हो सकता है?
- f-string तुरंत string में evaluate होता है, इसलिए user input शामिल करने वाले कोड में SQL Injection या XSS हो सकता है
- उदाहरण:
f"<div>{user_input}</div>"→ attack code सीधे insert हो सकता है
- उदाहरण:
- t-string इस evaluation को टाल देता है, जिससे इसे स्पष्ट processing के बाद ही इस्तेमाल किया जा सकता है
t-string के उपयोग के उदाहरण
-
HTML escape processing का उदाहरण:
evil = "<script>alert('bad')</script>" template = t"<p>{evil}</p>" safe = html(template) # safe है "<p><script>alert('bad')</script></p>" -
attribute auto-insertion जैसी अधिक जटिल processing भी संभव है:
attributes = {"src": "roquefort.jpg", "alt": "Yum"} template = t"<img {attributes} />" element = html(template) # परिणाम: "<img src='roquefort.jpg' alt='Yum' />"
संरचना और API
-
Templateobject,.stringsऔर.valuesproperties के जरिए मूल text और interpolated values को अलग-अलग उपलब्ध कराता है -
interpolationsproperty के जरिए!s,:>8जैसी format details तक भी पहुंचा जा सकता है -
iteration के जरिए text और values के मिले-जुले रूप को भी सीधे process किया जा सकता है
-
इसे manually भी बनाया जा सकता है:
from string.templatelib import Template, Interpolation template = Template( "Hello ", Interpolation(value="World", expression="name"), "!" )
एक दिलचस्प उदाहरण: Pig Latin converter
-
Templateobject की सामग्री पर iteration करके शब्दों को Pig Latin में बदलने का उदाहरण:def pig_latin(template: Template) -> str: ... name = "world" template = t"Hello {name}!" assert pig_latin(template) == "Hello orldway!"
आगे की दिशा
- t-strings, web/security-केंद्रित string processing में सुरक्षा और extensibility ला सकते हैं
- उम्मीद है कि
black,ruff, VS Code जैसे developer tools t-string formatting/highlighting को support करेंगे - यह JavaScript developers के परिचित tagged template approach जैसा है, इसलिए कई frameworks में इसके उपयोग की संभावना अधिक है
developer community के साथ सहयोग
- यह फीचर कई Python community members की भागीदारी और सहयोग से पूरा हुआ
- खास तौर पर Jim, Paul, Koudai, Lysandros, और Guido जैसे प्रमुख लोगों के साथ सहयोग का उल्लेख किया गया है
- PEP 750 और उसके example repository को GitHub पर देखा जा सकता है
Python 3.14 का t-strings फीचर string की सुरक्षा और extensibility दोनों को साथ लाता है, और मौजूदा f-string की सीमाओं से आगे बढ़ने वाली एक अहम छलांग है
2 टिप्पणियां
PEP 750 – टेम्पलेट स्ट्रिंग्स (t-strings) स्वीकृत
Hacker News राय
कुल मिलाकर, यह फीचर काफ़ी बढ़िया है। मूल रूप से, यह इस तरह के कोड को
इस तरह बदल देता है
सवाल यह है कि क्या यह syntactic sugar नए language feature की complexity को सही ठहराने लायक फ़ायदा देता है। मुझे लगता है कि इस मामले में हाँ, दो वजहों से
साथ ही, उम्मीद है कि tooling ecosystem भी t-strings को support करने के लिए ढल जाएगा। उदाहरण के लिए, अच्छा होगा अगर black और ruff t-string content को format करें, और vscode HTML या SQL जैसे common content types को colorize करे
क्या हम इस तरह का साफ़-सुथरा SQL syntax इस्तेमाल कर पाएँगे?
अगर db.get() function template स्वीकार करता है, तो हाँ। अब तक SQL इस्तेमाल करने के जितने तरीके मैंने देखे हैं, उनमें यह सबसे साफ़ तरीका होगा
व्यक्तिगत रूप से, मुझे लगता है कि यह फीचर एक सामान्य feature बनने के लिए बहुत specific problem पर केंद्रित है। Python लगातार बड़ा होता जा रहा है। जब लोग पूछते हैं कि क्या Python सीखना आसान और simple है, तो कहना पड़ता है, "बुनियादी बातें तो हैं, लेकिन पूरी language सीखना वैसा नहीं है"
बड़ी चर्चा (414 points, 10 दिन पहले, 324 comments) लिंक
काफ़ी बढ़िया। अगर JS features port किए जा रहे हैं, तो क्या अगला dictionary unpacking/destructuring मिल सकता है?
सिर्फ़ नए x-string feature को built-in बना देना थोड़ा "ट्रिक" जैसा लगता है। अच्छा होता अगर हम कुछ ऐसा कर पाते
2025 का Zen of Python:
2025 में Python string formatting:
मुझे समझ नहीं आता कि template पर function apply करना, f-string variable पर function apply करने से कैसे अलग है। यानी, यह करने के बजाय:
सीधे यह क्यों न करें:
या f-string बनाने से पहले। क्या बात सिर्फ़ इतनी है कि sanitization/string manipulation वाला हिस्सा भूला न जाए और उसे ज़बरदस्ती कराया जाए?
नमस्ते! यह लेख मैंने लिखा है :-)