[Python-3000] String formating operations in python 3k

Barry Warsaw barry at python.org
Wed Apr 5 04:16:18 CEST 2006


On Apr 4, 2006, at 7:44 PM, Greg Ewing wrote:

> I'm more interested in long-term readability
> and maintainability than in pandering to one-off
> impressions when coming from another language.
>
> I don't believe they would have any difficulty
> "getting" this, anyway. Can you honestly say
> that if you saw something like
>
>   subst("User {user} has printed {n} pages",
>     user = user_code, n = page_count)
>
> you wouldn't instantly figure out what was
> going on?
>
> There are other problems with the $ syntax too.
> The lack of an explicit closing marker causes
> problems when the parameter is immediately
> followed in the string by an alphanumeric
> character. You end up having to allow some
> kind of bracketing as an optional extra
> anyway, such as
>
>   subst("I'm a ${what}spotter", what = "train")
>
> Seems to me it's simpler as well as clearer
> to just make the syntax fully bracketed in the
> first place.
>

BTW, with string.Template's defaults, $what and ${what} are exactly  
equivalent.  IOW, totally optional so if your substitution variable  
is followed by "non-identifier" characters, just use $what, otherwise  
use ${what}.

I haven't tried, but string.Template might even support dropping the  
delimiter altogether, so that you could use just {what}.  Give it a  
shot and let us know! :)

-Barry



More information about the Python-3000 mailing list