Skip to main content

The Immutable Package

This package is an exceedingly simple wrapper around the builtin namedtuple from the collections package.

It allows you to instantiate via a tuple or via kwargs. It simplifies the case where you know ahead of time what the values of an Immutable should be and you just need to instantiate it once.

Install

pip install immutable

Details

namedtuple

The namedtuple is a Python builtin that allows you to instantiate an object as follows:

from collections import namedtuple

TupleFactory = namedtuple('ATuple', ['using', 'these', 'attrs'])
ATuple = TupleFactory('first', these='second', attrs='third')
ATuple  # ATuple(using='first', these='second', attrs='third')

# dot-access attributes
ATuple.using  # 'first'
Atuple.these  # 'second'
ATuple.attrs  # 'third'

# index-access attributes
ATuple[0]  # 'first'
ATuple[1]  # 'second'
ATuple[2]  # 'third'
ATuple[-1]  # 'third'

# the class name is as specified in creating the original factory
ATuple.__class__.__name__  # 'ATuple'

ImmutableFactory

Replicate namedtuple functionality

The ImmutableFactory is just a thin wrapper that allows you to do this in one step:

from immutable import ImmutableFactory

attributes = (('using', 'first'), ('these', 'second'), ('attrs', 'third'))

# don't worry about the extra kwargs for now :)
ATuple = ImmutableFactory.create(attributes, keys=False, values=False, items=False)
ATuple  # Immutable(using='first', these='second', attrs='third')

# dot-access attributes
ATuple.using  # 'first'
Atuple.these  # 'second'
ATuple.attrs  # 'third'

# index-access attributes
ATuple[0]  # 'first'
ATuple[1]  # 'second'
ATuple[2]  # 'third'
ATuple[-1]  # 'third'

# the class name is *always* `Immutable` now
ATuple.__class__.__name__  # 'Immutable'

Some extra bells and whistles (don’t get too excited)

Most of the time, we don’t care about the order. This allows us to instantiate in a much cleaner style:

from immutable import ImmutableFactory

ATuple = ImmutableFactory.create(using='first', these='second', attrs='third',
                                 keys=False, values=False, items=False)

# note that there's no predictable order here
ATuple  # Immutable(these='second', using='first', attrs='third')

# dot-access attributes
ATuple.using  # 'first'
Atuple.these  # 'second'
ATuple.attrs  # 'third'

# doesn't really make sense to index-access attributes now, so don't.

# the class name is *always* `Immutable` now
ATuple.__class__.__name__  # 'Immutable'

Additionally, it’s helpful to have dict-like keys, values, and items. These

Notes

Note if you use a mutable as a value for an attribute of an Immutable object, you’ll be able to change it. If this wasn’t the case, the ImmutableFactory would need to mutate your input data–not nice.

from immutable import ImmutableFactory

ATuple = ImmutableFactory.create(mutable=['a', 'list'])
ATuple.mutable  # ['a', 'list']
ATuple.mutable.append('can change!')
ATuple.mutable  # ['a', 'list', 'can change!']

Metadata

Release files for immutable 0.0.2

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

Source distribution (sdist)

Source distribution for immutable 0.0.2
File Size Uploaded
immutable-0.0.2.tar.gz 6.1 kB Details

Release files / immutable-0.0.2.tar.gz

Download URL immutable-0.0.2.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2b7d281e4e1cf782082bb02eeb8898a3bac76b237abec0ddb7c3fb4bca408670
BLAKE2b-256 checksum
How to use checksums
cdd194ebd56b425c8dc06f2a887a8b7333950ac7265de790a561e3fa0db85c45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

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