Skip to main content

Immutable dict and lists

Project description

pyimmutable

https://travis-ci.org/spacedentist/pyimmutable.svg?branch=master License: MIT

API documentation is hosted on GitHub pages.

Introduction

pyimmutable offers two immutable data types for Python3.6+: a mapping (similar to dict, but, obviously immutable) and a sequence (like list). It is implemented in C++, using the faboulous “immer” library for efficiently implementing the immutable data structures.

When your Python code passes an ImmutableDict or an ImmutableList to some function, you do not have to worry that function may alter the contents of that object. A reference to such an immutable data structure is guaranteed to forever point to the same data. If you do want to change the contents of your data, you have to update the reference. So instead of data["foo"] = "bar" you will have to write data = data.set("foo", "bar"). As a result, your data variable will be updated, but any other variable that referenced data before the change will keep referencing the old data—the one before the update.

Immutable data structures like this allow for some interesting optimization features. For example, two ImmutableDict objects with identical content are not actually two objects but one and the same object, and the same is true for ImmutableList. If any operation returns an ImmutableDict or an ImmutableList that is identical to one that has been constructed earlier, the existing one is returned instead of a new one being constructed. This entails that e.g. two ImmutableDict objects, even if they represent big trees of data, can be compared using the Python is operation.

pyimmutable has a stricter requirement for equality than the usual Python convention. In a nutshell, for pyimmutable, the integer 3 and the float 3.0 are not equal. An ImmutableDict can store different values for the keys 3 and 3.0, whereas a normal Python dict cannot. You can use objects of any type as keys and values, but only identical objects are considered equal. There is an exception to this rule for a small set of built-in types (int, float, str, bytes, tuple—all of which are immutable themselves), which are considered equal when they contain the same data.

Example

>>> from pyimmutable import ImmutableDict
>>> d1 = ImmutableDict()

d1 is an empty immutable dictionary.

>>> d2 = d1.set("foo", "bar")

d2 contains one key (“foo”) whose value is “bar”. d1 is remains an empty immutable dictionary.

>>> d3 = d2.set("fox", "baz")

d3 now contains two keys (“foo”, “fox”) with values “bar” and “baz”, respectively. d1 and d2 remain unchanged.

>>> d4 = ImmutableDict(foo="bar", fox="baz")

d4 is a newly constructed ImmutableDict, initialized with values foo and fox that happen to be the same as in d3. In such a case, pyimmutable does not actually construct a new ImmutableDict for us, but returns the d3 object from earlier.

>>> d3 is d4
True

This is fine, because ImmutableDict objects are immutable, and since the objects referred to by d3 and d4 can never be mutated and therefore will always have the same contents, d3 and d4 might as well refer to the same object.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyimmutable-0.2.1.tar.gz (90.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyimmutable-0.2.1-py3.7-linux-x86_64.egg (133.6 kB view details)

Uploaded Egg

File details

Details for the file pyimmutable-0.2.1.tar.gz.

File metadata

  • Download URL: pyimmutable-0.2.1.tar.gz
  • Upload date:
  • Size: 90.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.3

File hashes

Hashes for pyimmutable-0.2.1.tar.gz
Algorithm Hash digest
SHA256 35d36ec93b07a92603549039080e5b3e66df6e03d309aa2c8a414126169211ea
MD5 ac34fd7ff48d8e3b7832765e645a19fe
BLAKE2b-256 e7aa1d7cd70c50edba1072fed93640b0b34d56416c18f8953a7c729ccd388b0d

See more details on using hashes here.

File details

Details for the file pyimmutable-0.2.1-py3.7-linux-x86_64.egg.

File metadata

  • Download URL: pyimmutable-0.2.1-py3.7-linux-x86_64.egg
  • Upload date:
  • Size: 133.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.3

File hashes

Hashes for pyimmutable-0.2.1-py3.7-linux-x86_64.egg
Algorithm Hash digest
SHA256 43315bd721018e13889465abb807cc1c5e0e25a10eb42916e3e84deb6a0f9221
MD5 d64be4b0c910151cbe706beb057c640e
BLAKE2b-256 5b7294cad8212bfb71f63f2013d2193d459697cc3f3965e9a6ed4c8553889a2c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page