So I have gathered that PEP 750: Tag Strings For Writing Domain-Specific Languages - #313 by dg-pb abandoned custom tag syntax.
So I am thinking whether it would be useful to introduce it independently from string formatting.
E.g. In JupyterLite it was made to behave as follows:
def greet(*args):
print(args)
greet"Hello ?? {a:1}" # (DecodedConcrete('Hello ?? '), InterpolationConcrete(...))
What about making it generic?
def greet(string):
print(string)
greet"Hello ?? {a:1}" # "Hello ?? {a:1}"
Why would this be useful?
Essentially, this would provide a playground for users to implement various conveniences. Being able to do this, might provide a motivation for experimentation with ideas that would otherwise not happen due to inability to have attractive syntax off the shelf.
2 use-cases that come to mind:
- String formatting
Now, instead of receiving preprocessed interpolation, it can be implemented within (and user can implement whatever he likes):
def greet(string):
args = prepare_interpolation(string)
salutation = args[0].upper()
return f"{salutation}"
greet"Hello" # "HELLO"
- Conveniences for string-code.
E.g.:
a) subinterpreters
b) Backquotes for deferred expression - #114 by Paddy3118
For a simple eval:
def e(string):
return eval(string)
e"1 + 1"
Also, it could be used to implement conveniences for deferred evaluation without needing to make any changes to Python until it is clear that something specialised and hard coded/more performant is required.
E.g.
def d(string):
# maybe sys._getframe
return DeferedExpr(eval(f'lambda: {string}'))
a = 1
deferred = d"a + 1"
So this would potentially cover a certain chunk of various syntax change proposals for the sake of convenience.
Of course, API could be more robust than a simple function so that it wouldn’t take space in namespace.
In the same way that f"" does not shadow variable f.
E.g. by some sort of more sophisticated registry process.
Given PEP750 explored this, thought it might be worth suggesting this while concept is still fresh.