[docs] error in example : http://docs.python.org/release/3.1.3/library/stdtypes.html#dictionary-view-objects

Emil Jimenez ejimenez at lakeheadu.ca
Fri Jul 29 22:20:08 CEST 2011


In the "An example of dictionary view usage", the last output should
be "{'juice', 'sausage', 'bacon', 'spam'}", not "{'juice', 'eggs',
'bacon', 'spam'}"

-- PROPOSED CORRECT EXAMPLE --
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.keys()
>>> values = dishes.values()
>>>
>>> # iteration
... n = 0
>>> for val in values:
...     n += val
...
>>> print(n)
504
>>>
>>> # keys and values are iterated over in the same order
... list(keys)
['eggs', 'bacon', 'sausage', 'spam']
>>> list(values)
[2, 1, 1, 500]
>>>
>>> # view objects are dynamic and reflect dict changes
... del dishes['eggs']
>>> del dishes['sausage']
>>> list(keys)
['bacon', 'spam']
>>>
>>> # set operations
... keys & {'eggs', 'bacon', 'salad'}
{'bacon'}
>>> keys ^ {'sausage', 'juice'}
{'juice', 'sausage', 'bacon', 'spam'}
>>>


-- CURRENT INCORRECT EXAMPLE --

An example of dictionary view usage:

>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.keys()
>>> values = dishes.values()

>>> # iteration
>>> n = 0
>>> for val in values:
...     n += val
>>> print(n)
504

>>> # keys and values are iterated over in the same order
>>> list(keys)
['eggs', 'bacon', 'sausage', 'spam']
>>> list(values)
[2, 1, 1, 500]

>>> # view objects are dynamic and reflect dict changes
>>> del dishes['eggs']
>>> del dishes['sausage']
>>> list(keys)
['spam', 'bacon']

>>> # set operations
>>> keys & {'eggs', 'bacon', 'salad'}
{'bacon'}
>>> keys ^ {'sausage', 'juice'}
{'juice', 'eggs', 'bacon', 'spam'}


More information about the docs mailing list