classification
Title: In string.Template it's impossible to transform delimiter in the derived class
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: barry Nosy List: SilentGhost, barry, merwok, py.user, r.david.murray
Priority: normal Keywords:

Created on 2011-07-08 00:36 by py.user, last changed 2017-03-28 14:04 by barry. This issue is now closed.

Messages (7)
msg140010 - (view) Author: py.user (py.user) * Date: 2011-07-08 00:36
>>> import string
>>> class MyTemplate(string.Template):
...   delimiter = '.'
... 
>>> MyTemplate.delimiter = 'x'
>>> mt = MyTemplate('.field xfield')
>>> mt.substitute(field=None)
'None xfield'
>>> mt.delimiter
'x'
>>>


If I want to change the pattern string by any delimiter, I should create a new class for every delimiter

I would change the delimiter either in the object or in the class at any time
msg140038 - (view) Author: SilentGhost (SilentGhost) * Date: 2011-07-08 18:16
3.1 is in security fixes-only mode
msg140047 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-07-09 02:54
You are correct that this is a feature request as written.  I think there is also a doc bug (the docs should probably mention that changing delimiter after class creation is a no-op).

To add this feature the computation of the regex would need to be moved from the metaclass init to the object init.  The question is, was doing this in a metaclass a design decision (perhaps a micro-optimization?), or a "cute trick" that turns out to cause unintuitive consequences?  I think Barry was involved in adding the Template feature, so I'm adding him as nosy (I don't have a clone handy to check hg annotate).
msg140118 - (view) Author: √Čric Araujo (merwok) * (Python committer) Date: 2011-07-11 13:05
See 629200d880ea for answers.
msg140123 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2011-07-11 13:13
I don't know that anybody relies on the current behavior and computers are now a bazillion times faster than they were in 2004, so who needs that micro optimization any more?
msg290673 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2017-03-28 00:44
As this issue has been open for a long time, and I don't think it's worth changing the implementation, I am changing this to a documentation bug and will fix it along with the rewrites for bpo-19824 and bpo-20314
msg290718 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2017-03-28 14:02
New changeset 9f74deba784fc8781d13ed564f69c02ed7c331bb by Barry Warsaw in branch 'master':
Improve the documentation for template strings (#856)
https://github.com/python/cpython/commit/9f74deba784fc8781d13ed564f69c02ed7c331bb
History
Date User Action Args
2017-03-28 14:04:00barrysetstatus: open -> closed
resolution: fixed
stage: resolved
2017-03-28 14:02:09barrysetmessages: + msg290718
2017-03-28 00:44:19barrysetassignee: barry
messages: + msg290673
components: + Documentation, - Library (Lib)
versions: + Python 3.7, - Python 3.3
2011-07-11 13:13:51barrysetmessages: + msg140123
2011-07-11 13:05:12merwoksetnosy: + merwok
messages: + msg140118
2011-07-09 02:54:58r.david.murraysetnosy: + barry, r.david.murray

messages: + msg140047
versions: + Python 3.3, - Python 3.2
2011-07-08 18:16:58SilentGhostsetnosy: + SilentGhost

messages: + msg140038
versions: + Python 3.2, - Python 3.1
2011-07-08 00:36:49py.usercreate