[Python-Dev] Fwd: deep question re dict as formatting input

Steve Holden steve at holdenweb.com
Wed Feb 23 00:28:10 CET 2011


On Feb 22, 2011, at 3:08 PM, Eric Smith wrote:

> Quoting PEP 3101:
> 
> An example of the 'getitem' syntax:
> 
>        "My name is {0[name]}".format(dict(name='Fred'))
> 
> It should be noted that the use of 'getitem' within a format string
> is much more limited than its conventional usage.  In the above example,
> the string 'name' really is the literal string 'name', not a variable
> named 'name'.  The rules for parsing an item key are very simple.
> If it starts with a digit, then it is treated as a number, otherwise
> it is used as a string.
> 
That's not strictly true:

>>> d = {"Steve":"Holden", "Guido":"van Rossum", 21.2:"float"}
>>> d[21.1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 21.1
>>> d[21.2]
'float'
>>> "{0[21.2]}".format(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '21.2'
>>> 

But I take your point, and should have thought to read the PEP. Thanks!

Kirby: Please apologize to Ethan. I can't remember being aware of the PEP 3101 specification quoted by Eric above. We will probably need to modify the course materials to take this wrinkle into account (at least by demonstrating that we are aware of it).

regards
 Steve

> 
> On 2/22/2011 6:01 PM, Steve Holden wrote:
>> One of the students on an introductory Python 3 class asks a very good
>> question about string formatting. This could be because the course
>> materials are misleading, so I would like to understand. It would appear
>> from tests that "{0[X]}".format(...) first tries to convert the string
>> "X" to in integer. If it succeeds then __getitem__() is called with the
>> integer as an argument, otherwise it is called with the string itself as
>> an argument. Is this correct?
>> 
>> The documentation at
>> http://docs.python.org/library/string.html#formatspec is silent on
>> whether strings were ever intended to be used as subscripts. Does this
>> seem sensible? Was it considered during design? Should I alter the
>> materials so that only integer subscripts are used?
>> 
>> regards
>> Steve
>> 
>> 
>> Begin forwarded message:
>> 
>>> *From: *kirby urner <kirby.urner at gmail.com <mailto:kirby.urner at gmail.com>>
>>> *Date: *February 22, 2011 2:31:08 PM PST
>>> *To: *Steve Holden <steve at holdenweb.com <mailto:steve at holdenweb.com>>
>>> *Subject: **deep question re dict as formatting input*
>>> 
>>>>>> d
>>> {'Steve': 'Holden', 'Tim': 'Peters', 'Guido': 'van Rossum', '1':
>>> 'string', 1: 'integer'}
>>>>>> "{0[Guido]} is cool".format(d)
>>> 'van Rossum is cool'
>>>>>> "{0[1]} is cool".format(d)
>>> 'integer is cool'
>>>>>> "{0['1']} is cool".format(d)
>>> Traceback (most recent call last):
>>> File "<pyshell#19>", line 1, in <module>
>>> "{0['1']} is cool".format(d)
>>> KeyError: "'1'"
>>> 
>>> 
>>> Student question:
>>> 
>>> Good morning!
>>> 
>>> Question on .format(), interactive session follows:
>>> 
>>> --> d = {"Steve": "Holden",
>>> ... "Guido": "van Rossum",
>>> ... "Tim": "Peters",
>>> ... "1": "string",
>>> ... 1: "integer"}
>>> 
>>> --> d
>>> {'Steve': 'Holden', 'Tim': 'Peters', '1': 'string', 1: 'integer',
>>> 'Guido': 'van Rossum'}
>>> 
>>> --> d[1]
>>> 'integer'
>>> 
>>> --> d['1']
>>> 'string'
>>> 
>>> --> "{dct[1]}".format(dct=d)
>>> 'integer'
>>> 
>>> --> "{dct[Guido]}".format(dct=d)
>>> 'van Rossum'
>>> 
>>> --> "{dct['1']}".format(dct=d)
>>> Traceback (most recent call last):
>>> File "<console>", line 1, in <module>
>>> KeyError: "'1'"
>>> 
>>> Question: If {dct[Guido]} treats Guido as str, why doesn't {dct[1]}
>>> treate 1 as str? Feels like an automatic conversion from str to int.
>>> Furthermore, how does one access the key '1' in a format statement?
>>> 
>>> ~Ethan~
>> 
>> 
>> 
>> _______________________________________________
>> Python-Dev mailing list
>> Python-Dev at python.org
>> http://mail.python.org/mailman/listinfo/python-dev
>> Unsubscribe: http://mail.python.org/mailman/options/python-dev/eric%2Ba-python-dev%40trueblade.com
> 



More information about the Python-Dev mailing list