Python Glossary Русский

Python 3.3+collectionsterm

collections.abc.MappingView

collections.abc — protocols

The base class of dict views (KeysView/ValuesView/ItemsView).

Syntax

isinstance(obj, collections.abc.MappingView)

MappingView itself is nearly empty: it contributes only a length and a readable repr, while set operations and membership tests come from its subclasses KeysView, ValuesView and ItemsView, so you rarely reference it directly. The one thing the check tells you is that you hold a live window onto someone else's dict rather than a copy — wrap it in list() or set() when you need a frozen snapshot.

Examples

import collections.abc as abc
print(isinstance({}.keys(), abc.MappingView))   # → True
print(isinstance({}.values(), abc.MappingView))   # → True
print(isinstance({}.items(), abc.MappingView))   # → True
d = {'a': 1, 'b': 2}; print(len(d.keys()))   # → 2
print(isinstance([], abc.MappingView))   # → False
print(issubclass(abc.ItemsView, abc.MappingView))   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy