tags:

views:

40

answers:

3
#!/usr/bin/python

from string import 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

+7  A: 

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.

interjay
+1  A: 
print s.substitute(x='lee')
SilentGhost
+3  A: 

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.

Alex Martelli