itypes

1.2.0 · maintenance · verified Sat Apr 11

itypes is a Python library providing simple immutable container types and custom immutable objects. It prioritizes simplicity over performance, offering a way to create data structures that cannot be modified after creation. The current version is 1.2.0, released in April 2020, indicating a slow release cadence with limited recent development activity.

Warnings

Install

Imports

Quickstart

Demonstrates how to create and interact with immutable Configuration objects and custom immutable objects by subclassing itypes.Object. Highlights that direct modification is prevented, requiring new objects for 'changes'.

import itypes

# Immutable dictionary-like object
config = itypes.Configuration('worker-process', {'hostname': 'example.com', 'dynos': 4})
print(f"Original config title: {config.title}")
print(f"Original config dynos: {config.get('dynos')}")

# Attempting to change an attribute directly will raise an error
try:
    config.title = 'New Title'
except TypeError as e:
    print(f"Caught expected error: {e}")

# To 'modify', a new object is returned
new_config = config.set('dynos', 2)
print(f"New config dynos: {new_config.get('dynos')}")
print(f"Original config dynos (unchanged): {config.get('dynos')}")

# Custom immutable object
class Document(itypes.Object):
    def __init__(self, title, content):
        self._title = title
        self._content = content

    @property
    def title(self):
        return self._title

    @property
    def content(self):
        return self._content

doc = Document(title='Immutability', content='For simplicity')
print(f"Document title: {doc.title}")

# Attempting to change a public property directly will raise an error
try:
    doc.title = 'Changed Title'
except TypeError as e:
    print(f"Caught expected error on custom object: {e}")

view raw JSON →