By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,563 Members | 918 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,563 IT Pros & Developers. It's quick & easy.

string.Template.delimiter cannot be overriden?

P: n/a
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?

I've only been using Python for a couple of weeks so I'm not sure what
the best approach is here.

Thanks in advance.
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Jun 16, 9:53*pm, kretik <kre...@yahoo.comwrote:
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

Jun 27 '08 #2

P: n/a
Raymond Hettinger wrote:
On Jun 16, 9:53 pm, kretik <kre...@yahoo.comwrote:
>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.
OK, that makes no sense but I guess it's some sort of pythonistic thing
or something =)

Thanks a lot for the response.
Jun 27 '08 #3

P: n/a
En Tue, 17 Jun 2008 02:20:23 -0300, Raymond Hettinger <py****@rcn.com>
escribió:
On Jun 16, 9:53*pm, kretik <kre...@yahoo.comwrote:
>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.
Just out of curiosity, why was it done that way?
I'd say the "obvious" way to change the default delimiter would be to set
an instance attribute - so I guess this was a deliberate decision, but I
can't figure out why it is better this way...

--
Gabriel Genellina

Jun 27 '08 #4

P: n/a
[kretik]
>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?
[raymond]
Yes, subclassing is the intended way to produce variants of Template
with a different delimiter.
[gabriel]
Just out of curiosity, why was it done that way?
I'd say the "obvious" way to change the default delimiter would be to set *
an instance attribute - so I guess this was a deliberate decision, but I *
can't figure out why it is better this way...
IIRC, Barry and Tim came-up with the metaclass because the delimiter
and
pattern decisions are less granular than the creating of individual
templates.
Typically, the former decision is made once per application, but there
may
be thousands of template instances.

An earlier version of the API looked like this:

s = Template('Move %piece to %position', delimiter='%')
t = Template('%source takes %target', delimiter='%')

The repeated need to set the delimiter for every template was slow and
error-prone.

The newer API is:

class PercentTemplate(string.Template):
delimiter = '%'
s = PercentTemplate('Move %piece to %position')
t = PercentTemplate('%source takes %target')

So, basically it was just a factoring decision.

Instead of using a metaclass, one other possible choice would have
been to use a factory function:

PercentTemplate = template_maker(delimiter='%')
s = PercentTemplate('Move %piece to %position')
t = PercentTemplate('%source takes %target')

Raymond
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.