https://github.com/nolanbconaway/freezable_dict
A freezable dict object for python 3.
https://github.com/nolanbconaway/freezable_dict
dictionary immutability python3
Last synced: 8 months ago
JSON representation
A freezable dict object for python 3.
- Host: GitHub
- URL: https://github.com/nolanbconaway/freezable_dict
- Owner: nolanbconaway
- Created: 2018-12-27T19:12:47.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-14T00:23:49.000Z (about 7 years ago)
- Last Synced: 2025-09-22T15:22:46.069Z (8 months ago)
- Topics: dictionary, immutability, python3
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# freezable_dict
[](https://travis-ci.org/nolanbconaway/freezable_dict)
[](https://badge.fury.io/py/freezable-dict)
This repo provides a single class, a freezable dict, `FreezableDict`.
A `FreezableDict` is basically identical to a regular dict, but it offers the
additional possibility of freezing for use as a dictionary key, set item,
etc.
I stole most of the idea from https://stackoverflow.com/a/2704866 so props
to Mike (https://github.com/mikegraham).
## Install
Pypi:
```
pip install freezable_dict
```
For the bleeding edge:
```
pip install git+https://github.com/nolanbconaway/freezable_dict.git
```
## Usage
Import.
```python
>>> from freezable_dict import FreezableDict
```
Regular dict stuff works as usual.
```python
>>> d = FreezableDict(a=1, b=2)
>>> for k, v in d.items():
... print(k, v)
...
a 1
b 2
```
Freeze your dict and you can use it as a set item, dictionary key, etc.
But you can't mutate it.
```python
>>> d = FreezableDict(a=1, b=2).freeze()
>>> set([d])
{{'a': 1, 'b': 2}}
>>> try:
... d['c'] = 3
... except TypeError as e:
... print(e)
...
Frozen dicts cannot be changed! thaw to proceed.
```
Thaw out a frozen dict and you can mutate it as usual, but TypeError is
thrown when you try to use the frozen stuff.
```python
>>> d = FreezableDict(a=1, b=2).freeze()
>>> d.thaw()
{'a': 1, 'b': 2}
>>> d['c'] = 3
>>> try:
... set([d])
... except TypeError as e:
... print(e)
...
Thawed dicts do not hash! freeze me to proceed.
```