[Python-Dev] PEP 3101: Advanced String Formatting
Georg Brandl
g.brandl at gmx.net
Sun Apr 30 20:00:49 CEST 2006
Zachary Pincus wrote:
> I'm not sure about introducing a special syntax for accessing
> dictionary entries, array elements and/or object attributes *within a
> string formatter*... much less an overloaded one that differs from
> how these elements are accessed in "regular python".
Yes, I also think that's a bad idea.
>> Compound names are a sequence of simple names seperated by
>> periods:
>>
>> "My name is {0.name} :-\{\}".format(dict(name='Fred'))
And these escapes are also a bad idea. As it is, the backslashes stay in the
string, but it is not obvious to newcomers whether \{ is a general string
escape. The "right" way to do it would be "\\{", but that's becoming rather
longly. Why not use something like "{{"?
>> Compound names can be used to access specific dictionary entries,
>> array elements, or object attributes. In the above example, the
>> '{0.name}' field refers to the dictionary entry 'name' within
>> positional argument 0.
>
> Barring ambiguity about whether .name would mean the "name" attribute
> or the "name" dictionary entry if both were defined, I'm not sure I
> really see the point. How is:
> d = {last:'foo', first:'bar'}
> "My last name is {0.last}, my first name is {0.first}.".format(d)
>
> really that big a win over:
> d = {last:'foo', first:'bar'}
> "My last name is {0}, my first name is {1}.".format(d['last'], d
> ['first'])
Or even:
d = {last:'foo', first:'bar'}
"My last name is {last}, my first name is {first}.".format(**d)
Georg
More information about the Python-Dev
mailing list