[Python-ideas] Escaped braces in format specifiers

Eric V. Smith eric at trueblade.com
Thu May 17 12:08:25 EDT 2018


> On May 17, 2018, at 9:56 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
>> On 15 May 2018 at 16:23, Eric V. Smith <eric at trueblade.com> wrote:
>> I'm busy at the sprints, so I don't have a lot of time to think about this.
>> 
>> However, let me just say that recursive format specs are supported, to a depth of 1.
>> 
>> >>> width=10
>> >>> f'{"test":{width}}'
>> 'test      '
>> 
>> So first the string is basically expanded to:
>> f'{"test":10}'
>> Then the string is formatted again to produce the final result.
>> 
>> That is why the braces must match: they're being used for recursive format specs. There's no mechanism for having braces that aren't inspected by the f-string machinery.
> 
> https://www.python.org/dev/peps/pep-0536/ also seems worth noting (I don't actually understand the specifics of that PEP myself,  just making sure that Ken's aware of its existence if this is an area of interest)

Yes, that’s a good point. Thanks for remembering this PEP. 

I disagree with much of the PEP and it’s language, such as “These limitations serve no purpose ...”. And I especially object to the fact that it doesn’t take in to account the effect of its proposed changes on other python implementations or on tools like editors, linters, static type checkers, etc., all of which would have to be modified. 

Not that those are insurmountable issues or mean that we should never improve f-strings, but they do need to be carefully considered and given weight in the decision process. 

Eric

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180517/9106ab17/attachment.html>


More information about the Python-ideas mailing list