Uh oh!
There was an error while loading. Please reload this page.
gh-118761: Optimise import time for string - #132037
Conversation
picnixz
left a comment
There was a problem hiding this comment.
I'm very happy with this because I need to access many times string.* constants without needing the Template class so it's a very good optimization.
Uh oh!
There was an error while loading. Please reload this page.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Interesting idea. The code looks intimidating, but it might work.
How the help output looks now?
Is __init_subclass__() needed anymore?
Uh oh!
There was an error while loading. Please reload this page.
AA-Turner
commented
Apr 7, 2025
Help output looks fine: >>> from string import Template
>>> assert Template.flags isNone
>>> help(Template)
Help on class Template in module string:
class Template(builtins.object)
| Template(template)
|
| A string class for supporting $-substitutions.
|
| Methods defined here:
|
| __init__(self, template)
| Initialize self. See help(type(self)) for accurate signature.
|
| get_identifiers(self)
|
| is_valid(self)
|
| safe_substitute(self, mapping={}, /, **kws)
|
| substitute(self, mapping={}, /, **kws)
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __init_subclass__()
| This method is called when a class is subclassed.
|
| The default implementation does nothing. It may be
| overridden to extend subclasses.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| braceidpattern = None
|
| delimiter = '$'
|
| flags = re.IGNORECASE
|
| idpattern = '(?a:[_a-z][_a-z0-9]*)'
|
| pattern = re.compile('\n \\$(?:\n ...identifie...
>>> |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
It perhaps could be made simpler with classmethod + property, but the future of this feature is not clear.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This PR achieves a 27x improvement in import time for the
stringmodule. The main improvement comes from replacingTemplate.__init_subclass__()(GH-16256) with a descriptor class, allowing lazy import of theremodule.Current:
This PR: