[Python-ideas] template strings

MRAB python at mrabarnett.plus.com
Tue Aug 18 02:35:00 CEST 2015


On 2015-08-18 01:24, Yury Selivanov wrote:
> On 2015-08-17 7:24 PM, MRAB wrote:
>> On 2015-08-17 21:13, Yury Selivanov wrote:
>>> In ECMAScript 6 there is a concept of Template Strings [1]. What if
>>> we add
>>> something similar in Python?
>>>
>>> Some key ideas
>>> --------------
>>>
>>> 1. Template Strings (TS) will be built on top of PEP 498 machinery (if
>>> accepted).
>>>
>>> 2. The syntax will match the following:
>>>
>>>       {python identifier}{optional whitespace}{string literal}
>>>
>>> where "python identifier" can be any valid python name *except* r, u, b,
>>> or f.
>>>
>> [snip]
>>
>> What happens if you accidentally omit a comma?
>>
>>     print(count ' items found')
>>
>> Currently it's a syntax error, but, with this proposal, it becomes a
>> runtime error:
>>
>>     AttributeError: 'int' object has no attribute '__format_str__'
>>
>
> Exactly.  We can also (probably) use symtable to lookup 'count'
> name and throw a SyntaxError if it's not defined.
>
What if it's being used in global (module) scope and someone is 
injecting 'count'
from outside, or, indeed, dynamically?



More information about the Python-ideas mailing list