string.Template.delimiter cannot be overriden?

K

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?

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.
 
R

Raymond Hettinger

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.
delimiter = "%"

'Move pawn to K4'


Raymond
 
K

kretik

Raymond said:
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.
 
G

Gabriel Genellina

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...
 
R

Raymond Hettinger

[kretik]
I've been trying to coax this class to use something other than the
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top