FrozenOrderedDict

About

FrozenOrderedDict is a immutable wrapper around an OrderedDict. It is similar to frozendict, and with regards to immutability it solves the same problems:

  • Because dictionaries are mutable, they are not hashable and cannot be used in sets or as dictionary keys.

  • Nasty bugs can and do occur when mutable data structures are passed around.

It can be initialized just like a dict or OrderedDict. Once instantiated, an instance of FrozenOrderedDict cannot be altered, since it does not implement the MutableMapping interface.

FrozenOrderedDict implements the Mapping interface, so can be used like a normal dictionary in most cases.

In order to modify the contents of a FrozenOrderedDict, a new instance must be created. The easiest way to do that is by calling the .copy() method. It will return a new instance of FrozenOrderedDict initialized using the following steps:

  1. A copy of the wrapped OrderedDict instance will be created.

  2. If any arguments or keyword arguments are passed to the .copy() method, they will be used to create another OrderedDict instance, which will then be used to update the copy made in step #1.

  3. Finally, self.__class__() will be called, passing the copy as the only argument.

API Reference

class FrozenOrderedDict(*args, **kwargs)[source]

Bases: FrozenBase[~KT, ~VT]

An immutable OrderedDict. It can be used as a drop-in replacement for dictionaries where immutability is desired.

Methods:

__contains__(key)

Return key in self.

__getitem__(key)

Return self[key].

copy(*args, **kwargs)

Return a copy of the FrozenOrderedDict.

get(k[, default])

Return the value for k if k is in the dictionary, else default.

items()

Returns a set-like object providing a view on the FrozenOrderedDict's items.

keys()

Returns a set-like object providing a view on the FrozenOrderedDict's keys.

values()

Returns an object providing a view on the FrozenOrderedDict's values.

__contains__(key)[source]

Return key in self.

Parameters

key (object)

Return type

bool

__getitem__(key)[source]

Return self[key].

Parameters

key (~KT)

Return type

~VT

copy(*args, **kwargs)[source]

Return a copy of the FrozenOrderedDict.

Parameters
  • args

  • kwargs

get(k, default=None)[source]

Return the value for k if k is in the dictionary, else default.

Parameters
  • k – The key to return the value for.

  • default – The value to return if key is not in the dictionary. Default None.

Overloads
items()[source]

Returns a set-like object providing a view on the FrozenOrderedDict's items.

Return type

AbstractSet[Tuple[~KT, ~VT]]

keys()[source]

Returns a set-like object providing a view on the FrozenOrderedDict's keys.

Return type

AbstractSet[~KT]

values()[source]

Returns an object providing a view on the FrozenOrderedDict's values.

Return type

ValuesView[~VT]