Skip to content

Add Template(strings=..., interpolations=...) APIΒ #139365

@da-woods

Description

@da-woods

Feature or enhancement

Proposal:

Internally, you store strings and interpolations as separate attributes. However the constructor to Template requires them to be passed as a long tuple of positional arguments (which then gets manually separated out).

For the likely Cython implementation it (probably) also makes sense to generate the strings and templates separately. For CPython modules it's possible to get _PyTemplate_Build out of the internal headers and use that. However that doesn't help for the Limited API.

In this case we have to go go through the Python constructor of Template - that's reasonable except that we have to do a lot of work zipping strings and interpolations into a single tuple so that the constructor of Template can then undo all the work putting them back.

It would be useful to have an interface to construct a Template from two iterables of strings and interpolations. I'd be happy with this being a Python API (rather than a C API). My initial proposal would be to define Template.__new__ as Template.__new__(cls, *args, strings=None, interpolations=None) and make it an error to pass both args and the keyword arguments in the same call.

Obviously there's multiple ways to implement Template and in future you might prefer not to separate the two. In this case the API I'm proposing would still work but no longer be the most efficient way to build it - I don't think this is a problem.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions