What is this syntax ?

Claudiu Popa cpopa at bitdefender.com
Mon Jun 20 03:18:45 EDT 2011


Hello,

Isn't this similar to php interpolation? And quite readable imo.

>>> import string
>>> template = string.Template("$scheme://$host:$port/$route#$fragment")
>>> template.substitute(scheme="http", host="google.com", port="80", route="", fragment="")
'http://google.com:80/#'
>>>


Roy Smith wrote:

> There's something nice about building up strings in-line, as
> opposed to having to look somewhere to see what's being interpolated.
> To give a more complex example, consider:
> 
> print "$scheme://$host:$port/$route#$fragment"
> 
> That certainly seems easier to me to read than:
> 
> print "%s://%s:%s/%s#%s" % (scheme,
>                             port,
>                             host,
>                             route,
>                             fragment)

  





More information about the Python-list mailing list