How is this advantageous over str.format?
How is this advantageous over str.format?
Posted Jan 23, 2025 15:16 UTC (Thu) by siddh (subscriber, #169663)In reply to: How is this advantageous over str.format? by rrolls
Parent article: A revamped Python string-formatting proposal
What I meant was
def html(t_str):
final = ""
for item in template:
match item:
case str() as s:
final += s
case Interpolation() as i:
final += sanitise(i)
return final
evil = "[script]alert('evil')[script]"
template = t"[p]{evil}[/p]"
sanitised = html(template)
is equivalent to
def html(given, args):
for k, v in args.items():
args[k] = sanitise(v)
return given_str.format(**args)
args = {"evil": "[script]alert('evil')[/script]"}
template = "[p]{evil}[/p]"
sanitised = html(template, args)
Whatever you said is equally applicable to the second case IIUC.
(Used [] instead of <> since LWN HTML comment parses it...)
