Python Glossary Русский

Python 3.3+collectionsterm

collections.abc.KeysView

collections.abc — protocols

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

Syntax

isinstance(obj, collections.abc.KeysView)

A keys view is a live window on the dict, not a snapshot: keys added later show up immediately, and mutating the dict while iterating the view raises RuntimeError. Unlike the values view, it is set-like and supports &, |, -, ^, so the shared keys of two dicts come straight from d1.keys() & d2.keys() with no intermediate set().

Examples

import collections.abc as abc
print(isinstance({}.keys(), abc.KeysView))   # → True
print(isinstance({}.values(), abc.KeysView))  # → False
print(list({'a': 1, 'b': 2}.keys()))  # → ['a', 'b']
print({'a': 1, 'b': 2}.keys() & {'b', 'c'})  # → {'b'}
print(isinstance({}.keys(), abc.Set))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy