2.1. DictView class

class immutable_views.DictView(a_dict)[source]

An immutable dictionary view.

Derived from Mapping.

This class provides an immutable view on a possibly mutable mapping object. The mapping object must be an instance of Mapping.

This can be used for example when a class maintains a dictionary that should be made available to users of the class without allowing them to modify the dictionary.

In the description of this class, the term ‘view’ always refers to the DictView object, and the term ‘dictionary’ or ‘original dictionary’ refers to the mapping object the view is based on.

The DictView class supports the complete behavior of Python class dict, except for any methods that would modify the dictionary. Note that the non-modifying methods of class dict are a superset of the methods defined for the abstract class Mapping (the methods are listed in the table at the top of the linked page).

The view is “live”: Since the view class delegates all operations to the original dictionary, any modification of the original dictionary object will be visible in the view object.

Note that only the view object is immutable, not its items. So if the values in the original dictionary are mutable objects, they can be modified through the view.

Note that in Python, augmented assignment (e.g. += is not guaranteed to modify the left hand object in place, but can result in a new object. For details, see object.__iadd__(). The += operator on a left hand object that is a DictView object results in a new DictView object on a new dictionary object.

Parameters

a_dict (Mapping) – The original dictionary. If this object is a DictView, its original dictionary is used.

Methods:

__contains__(key)

value in self: Return a boolean indicating whether the dictionary contains a value.

__eq__(other)

self == other: Return a boolean indicating whether the dictionary is equal to the other dictionary.

__ge__(other)

self >= other: Return a boolean indicating whether the dictionary is an inclusive superset of the other dictionary.

__getitem__(key)

self[key]: Return the value of the dictionary item with an existing key.

__gt__(other)

self > other: Return a boolean indicating whether the dictionary is a proper superset of the other dictionary.

__iter__()

Return an iterator through the dictionary keys.

__le__(other)

self <= other: Return a boolean indicating whether the dictionary is an inclusive subset of the other dictionary.

__len__()

len(self): Return the number of items in the dictionary.

__lt__(other)

self < other: Return a boolean indicating whether the dictionary is a proper subset of the other dictionary.

__ne__(other)

self != other: Return a boolean indicating whether the dictionary is not equal to the other dictionary.

__repr__()

repr(self): Return a string representation of the view suitable for debugging.

__reversed__()

reversed(self) ...: Return an iterator through the dictionary in reversed iteration order.

copy()

Return a new view on a shallow copy of the dictionary.

get(key[, default])

Return the value of the dictionary item with an existing key or a default value.

has_key(key)

Python 2 only: Return a boolean indicating whether the dictionary contains an item with the key.

items()

Return the dictionary items.

iteritems()

Python 2 only: Return an iterator through the dictionary items.

iterkeys()

Python 2 only: Return an iterator through the dictionary keys.

itervalues()

Python 2 only: Return an iterator through the dictionary values.

keys()

Return the dictionary keys.

values()

Return the dictionary values.

viewitems()

Python 2 only: Return a view on the dictionary items.

viewkeys()

Python 2 only: Return a view on the dictionary keys.

viewvalues()

Python 2 only: Return a view on the dictionary values.

__contains__(key)[source]

value in self: Return a boolean indicating whether the dictionary contains a value.

The return value indicates whether the original dictionary contains an item that is equal to the value.

__eq__(other)[source]

self == other: Return a boolean indicating whether the dictionary is equal to the other dictionary.

The return value indicates whether the items in the original dictionary are equal to the items in the other dictionary (or in case of a DictView, its original dictionary).

The other object must be a dict or DictView.

Raises

TypeError – The other object is not a dict or DictView.

__ge__(other)[source]

self >= other: Return a boolean indicating whether the dictionary is an inclusive superset of the other dictionary.

The return value indicates whether every item in the other dictionary (or in case of a DictView, its original dictionary) is in the original dictionary.

The other object must be a dict or DictView.

Raises

TypeError – The other object is not a dict or DictView.

__getitem__(key)[source]

self[key]: Return the value of the dictionary item with an existing key.

Raises

KeyError – Key does not exist.

__gt__(other)[source]

self > other: Return a boolean indicating whether the dictionary is a proper superset of the other dictionary.

The return value indicates whether the original dictionary is a proper superset of the other dictionary (or in case of a DictView, its original dictionary).

The other object must be a dict or DictView.

Raises

TypeError – The other object is not a dict or DictView.

__iter__()[source]

Return an iterator through the dictionary keys.

__le__(other)[source]

self <= other: Return a boolean indicating whether the dictionary is an inclusive subset of the other dictionary.

The return value indicates whether every item in the original dictionary is in the other dictionary (or in case of a DictView, its original dictionary).

The other object must be a dict or DictView.

Raises

TypeError – The other object is not a dict or DictView.

__len__()[source]

len(self): Return the number of items in the dictionary.

The return value is the number of items in the original dictionary.

__lt__(other)[source]

self < other: Return a boolean indicating whether the dictionary is a proper subset of the other dictionary.

The return value indicates whether the original dictionary is a proper subset of the other dictionary (or in case of a DictView, its original dictionary).

The other object must be a dict or DictView.

Raises

TypeError – The other object is dict a set or DictView.

__ne__(other)[source]

self != other: Return a boolean indicating whether the dictionary is not equal to the other dictionary.

The return value indicates whether the items in the original dictionary are not equal to the items in the other dictionary (or in case of a DictView, its original dictionary).

The other object must be a dict or DictView.

Raises

TypeError – The other object is not a dict or DictView.

__repr__()[source]

repr(self): Return a string representation of the view suitable for debugging.

The original dictionary is represented using its repr() representation.

__reversed__()[source]

reversed(self) ...: Return an iterator through the dictionary in reversed iteration order.

The returned iterator yields the items in the original dictionary in the reversed iteration order.

copy()[source]

Return a new view on a shallow copy of the dictionary.

The returned DictView object is a new view object on a dictionary object of the type of the original dictionary.

If the dictionary type is immutable, the returned dictionary object may be the original dictionary object. If the dictionary type is mutable, the returned dictionary is a new dictionary object that is a shallow copy of the original dictionary object.

get(key, default=None)[source]

Return the value of the dictionary item with an existing key or a default value.

has_key(key)[source]

Python 2 only: Return a boolean indicating whether the dictionary contains an item with the key.

Raises

AttributeError – The method does not exist on Python 3.

items()[source]

Return the dictionary items.

The items of the original dictionary are returned in iteration order and as a view in Python 3 and as a list in Python 2. Each returned item is a tuple of key and value.

See Dictionary View Objects on Python 3 for details about view objects.

iteritems()[source]

Python 2 only: Return an iterator through the dictionary items. Each item is a tuple of key and value.

Raises

AttributeError – The method does not exist on Python 3.

iterkeys()[source]

Python 2 only: Return an iterator through the dictionary keys.

Raises

AttributeError – The method does not exist on Python 3.

itervalues()[source]

Python 2 only: Return an iterator through the dictionary values.

Raises

AttributeError – The method does not exist on Python 3.

keys()[source]

Return the dictionary keys.

The keys of the original dictionary are returned in iteration order and as a view in Python 3 and as a list in Python 2.

See Dictionary View Objects on Python 3 for details about view objects.

values()[source]

Return the dictionary values.

The values of the original dictionary are returned in iteration order and as a view in Python 3 and as a list in Python 2.

See Dictionary View Objects on Python 3 for details about view objects.

viewitems()[source]

Python 2 only: Return a view on the dictionary items.

The items of the original dictionary are returned in iteration order and as a view. Each returned item is a tuple of key and value.

See Dictionary View Objects on Python 2 for details about view objects.

Raises

AttributeError – The method does not exist on Python 3.

viewkeys()[source]

Python 2 only: Return a view on the dictionary keys.

The keys of the original dictionary are returned in iteration order and as a view.

See Dictionary View Objects on Python 2 for details about view objects.

Raises

AttributeError – The method does not exist on Python 3.

viewvalues()[source]

Python 2 only: Return a view on the dictionary values.

The values of the original dictionary are returned in iteration order and as a view.

See Dictionary View Objects on Python 2 for details about view objects.

Raises

AttributeError – The method does not exist on Python 3.