[Python-3000] PEP 3101 Updated
Andrew James Wade
andrew.j.wade at gmail.com
Thu Aug 23 05:15:56 CEST 2007
On Wed, 22 Aug 2007 21:33:19 -0400
Eric Smith <eric+python-dev at trueblade.com> wrote:
> Ron Adam wrote:
...
> > 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.
What about:
>>> "{0:{lb}{1}{lb}}".format(ShowSpec(), 'abc', lb='{', rb='}')
'{abc}'
Ugly, but better than nothing.
> > 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.
Not a problem with recursion:
$ echo $(echo $(pwd))
/home/ajwade
$ a='echo $(pwd)'
$ echo $a
echo $(pwd)
$ echo $($a)
$(pwd)
$ echo $($($a))
bash: $(pwd): command not found
The key is to do substitution only once at each level of recursion;
which is what a naive recursive algorithm would do anyway. And I'd do
the recursive substitution before even starting to parse the field:
it's simple and powerful.
-- Andrew
More information about the Python-3000
mailing list