开发者

Template strings python 2.5 error

开发者 https://www.devze.com 2022-12-28 22:25 出处:网络
#!/usr/bin/python from string i开发者_运维问答mport Template s = Template(\'$x, go home $x\') s.substitute(x=\'lee\')
#!/usr/bin/python

from string i开发者_运维问答mport Template

s = Template('$x, go home $x')
s.substitute(x='lee')

print s

error i get is

<string.Template object at 0x81abdcc>

desired results i am looking for is : lee, go home lee


You need to look at the return value of substitute. It gives you the string with substitutions performed.

print s.substitute(x='lee')

The template object itself (s) is not changed. This gives you the ability to perform multiple substitutions with the same template object.


You're not getting an error: you're getting exactly what you're asking for -- the template itself. To achieve your desired result,

print s.substitute(x='lee')

Templates, like strings, are not mutable objects: any method you call on a template (or string) can never alter that template -- it can only produce a separate result which you can use. This, of course, applies to the .substitute method. You're calling it, but ignoring the result, and then printing the template -- no doubt you expect the template itself to be somehow altered, but that's just not how it works.


print s.substitute(x='lee')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号