Skip to main content

Frozen

Frozen creates an immutable reference to an object. This works (lazily) recursive: mutable members are also frozen upon access. It keeps the original object mutable, returning a proxy class that behaves just like the object except being uneditable. If the underlying object changes, the proxy will reflect this automatically. As such it is immutable in the sense that the reference can’t be used to change it, not in the sense that it cannot be changed at all (you could of course discard the original mutable reference). Frozen objects can not be used as dictionary keys.

Installation

  1. pip install frozenobj

  2. profit

Usage

Usage is very simple:

from frozenobj import frozen
immutable_obj = frozen(mutable_obj)

As a simple example:

class Cls:
        def __init__(self):
                self.li = [37, 42]
original = Cls()
freezered = frozen(original)
original.li.append(99)   # works
freezered.li.append(99)  # ImmutableError

Have a look at tests.py for more examples.

Notes

  • Someone really determined to change your proxy object can find ways to do so. This is not a security measure, it’s just a way to enforce your interface (in the purely imaginary case that people don’t read or follow the manual saying they shouldn’t change things).

  • It should work for most objects. It works for classes, class instances, modules, lists, sets, dictionaries and some types derived from them. It implicitly works for ints, floats, strings and tuples which are always immutable (they are returned, not proxied). Functions and methods are also considered constants.

  • The frozen method is idempotent and uses caching: if you call it twice on the same object, either successively or in parallel, you get the same proxy.

  • The recursive freezing on access, while not completely free, should have fairly low computational cost (you need store only values that are accessed a great many times).

  • Heavily relies on the lazy-object-proxy package, which is nice.

License

Frozen is available under the revised BSD license, see LICENSE.txt. You can do anything as long as you include the license, don’t use my name for promotion and are aware that there is no warranty.

Metadata

Release files for frozenobj 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for frozenobj 1.0
File Size Uploaded
frozenobj-1.0.tar.gz 4.7 kB Details

Release files / frozenobj-1.0.tar.gz

Download URL frozenobj-1.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3147136487cb67baf736e032c003adbd4fc658e867ff4b77f92a2957199239c8
BLAKE2b-256 checksum
How to use checksums
2ebc14fb404e96c28681be9b9a1bf330244bdcddd2fa8ff722965b25cdca1864
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page