string.Template.delimiter cannot be overriden?

Raymond Hettinger python at rcn.com
Tue Jun 17 01:20:23 EDT 2008


On Jun 16, 9:53 pm, kretik <kre... at yahoo.com> wrote:
> I've been trying to coax this class to use something other than the
> default '$' but it seems setting it to something else has no discernible
> effect. Is it necessary to inherit from the class to do this?

Yes, subclassing is the intended way to produce variants of Template
with a different delimiter.

>>> import string
>>> class PercentTemplate(string.Template):
	delimiter = "%"


>>> s = PercentTemplate('Move %piece to %position')
>>> s.substitute(piece='pawn', position='K4')
'Move pawn to K4'


Raymond




More information about the Python-list mailing list