[Python-3000] PEP 3101 Updated

Eric Smith eric+python-dev at trueblade.com
Thu Aug 23 03:33:19 CEST 2007


Ron Adam wrote:
> 
> 
> Eric Smith wrote:
>> Ron Adam wrote:
>>>> I've been re-reading the PEP, in an effort to make sure everything 
>>>> is working.  I realized that these tests should not pass.  The PEP 
>>>> says that "Format specifiers can themselves contain replacement 
>>>> fields".  The tests above have replacement fields in the field name, 
>>>> which is not allowed.  I'm going to remove this functionality.
>>>>
>>>> I believe the intent is to support a replacement for:
>>>> "%.*s" % (4, 'how now brown cow')
>>>>
>>>> Which would be:
>>>> "{0:.{1}}".format('how now brown cow', 4)
>>>>
>>>> For this, there's no need for replacement on field name.  I've taken 
>>>> it out of the code, and made these tests in to errors.
>>>
>>> I think it should work myself, but it could be added back in later if 
>>> there is a need to.
>>>
>>>
>>> I'm still concerned about the choice of {{ and }} as escaped brackets.
>>>
>>> What does the following do?
>>>
>>>
>>> "{0:{{^{1}}".format('Python', '12')
>>
>>  >>> "{0:{{^{1}}".format('Python', '12')
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> ValueError: unterminated replacement field
> 
> When are the "{{" and "}}" escape characters replaced with  '{' and '}'?

While parsing for the starting '{'.  I'm not saying this is the best or 
only or even PEP-specified way of doing it, but that's how the sample 
implementation does it (and the way the sandbox version has done it for 
many months).

>> But,
>>  >>> "{{{0:^{1}}".format('Python', '12')
>> '{   Python   '
> 
> So escaping '{' with '{{' and '}' with '}}' doesn't work inside of 
> format expressions?

As I have it implemented, yes.

> That would mean there is no way to pass a brace to a __format__ method.

No way using string.format, correct.  You could pass it in using the 
builtin format(), or by calling __format__ directly.  But you're 
correct, for the most part if string.format doesn't accept it, it's not 
practical.

>> I think you mean:
>> ShowSpec("{0:{1}}").format('abc', 'xyz')
> 
> No, because you may need to be able to pass the '{' and '}' character to 
> the format specifier in some way.  The standard specifiers don't use 
> them, but custom specifiers may need them.

Also true.

>>> "{0}".format('{value:{{^{width}}', width='10', value='Python')
>>
>>  >>> "{0}".format('{value:{{^{width}}', width='10', value='Python')
>> '{value:{{^{width}}'
> 
> Depending on weather or not the evaluation is recursive this may or may 
> not be correct.
> 
> I think it's actually easier to do it recursively and not put limits on 
> where format specifiers can be used or not.

But then you'd always have to worry that some replaced string looks like 
something that could be interpreted as a field, even if that's not what 
you want.

What if "{value}" came from user supplied input?  I don't think you'd 
want (or expect) any string you output that contains braces to be expanded.


More information about the Python-3000 mailing list