Python Glossary Русский

Python 3.3+collectionsterm

collections.abc.ItemsView

collections.abc — protocols

The type of a dict items view (d.items()).

Syntax

isinstance(obj, collections.abc.ItemsView)

An items view is a live window on the dict rather than a copy: updates to the dict show up immediately, but inserting or deleting keys while looping over items() aborts the iteration with RuntimeError. Less obvious is that items() is set-like and supports &, |, - and ^ — though only while every value is hashable; values() never offers this.

Examples

import collections.abc as abc
print(isinstance({}.items(), abc.ItemsView))   # → True
print(isinstance({}.keys(), abc.ItemsView))  # → False
print(list({'a': 1, 'b': 2}.items()))  # → [('a', 1), ('b', 2)]
print(('a', 1) in {'a': 1, 'b': 2}.items())  # → True
print(isinstance({}.items(), abc.Set))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy