[Python-ideas] template strings
Andrew Barnert
abarnert at yahoo.com
Tue Aug 18 02:45:44 CEST 2015
On Aug 17, 2015, at 17:24, Yury Selivanov <yselivanov.ml at gmail.com> 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.
Wouldn't that mean you can only use locals as prefixes, which seems to defeat the purpose?
More importantly, in the example, count _is_ defined, it's just defined as an int rather than a string-thingifying-thing.
More information about the Python-ideas
mailing list