Install & Compatibility
Where this runs
tested against v1.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 1.5s · import 0.007s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
namedlist
✓ from namedlist import namedlist
FACTORY
✓ from namedlist import namedlist, FACTORY
FACTORY is used for mutable default values to prevent shared state.
This quickstart demonstrates creating a basic mutable namedlist, using global default values for fields, and correctly handling mutable default values (like lists) using the `FACTORY` function to ensure each instance gets its own independent mutable object.
from namedlist import namedlist, FACTORY
# Basic mutable namedlist
Point = namedlist('Point', 'x y')
p = Point(1, 3)
print(f"Initial point: {p}")
p.x = 2
print(f"Modified point: {p}")
assert p.x == 2
assert p.y == 3
# Namedlist with default values
Rect = namedlist('Rect', 'x1 y1 x2 y2', default=0)
r = Rect(x1=10, y1=10)
print(f"Rectangle with defaults: {r}")
assert r.x1 == 10 and r.y1 == 10 and r.x2 == 0 and r.y2 == 0
# Namedlist with a factory for mutable defaults (e.g., list)
Config = namedlist('Config', [('name', 'default'), ('options', FACTORY(list))])
c1 = Config(name='App1')
c1.options.append('verbose')
print(f"Config 1: {c1}")
c2 = Config(name='App2') # new instance, new list for options
print(f"Config 2: {c2}")
assert c1.options == ['verbose']
assert c2.options == [] # Demonstrates independent mutable defaults
Debug
Known issues
deprecatedThe `namedlist` package is no longer actively maintained. For new projects, it is strongly recommended to use Python's built-in `dataclasses` module, which offers similar functionality with modern Python features and active development.fixMigrate to `dataclasses`. For example, a `namedlist` can often be directly replaced by a `@dataclass`.
affects: All versions, especially 1.8+
gotchaWhen defining `namedlist.namedlist` fields with mutable default values (e.g., lists, dictionaries), all instances created will share the *same* mutable object, leading to unexpected side effects when one instance modifies it.fixUse the `FACTORY` function provided by `namedlist` to wrap mutable default values. `FACTORY(list)` or `FACTORY(lambda: [1, 2])` will ensure a new mutable object is created for each instance.
affects: All versions of `namedlist`
gotchaBy default, `namedlist.namedlist` classes use `__slots__` to optimize memory usage. This prevents adding new attributes to an instance after creation, resulting in an `AttributeError` if attempted.fixIf you need to add new attributes to instances, create the `namedlist` with `use_slots=False`: `Point = namedlist('Point', 'x y', use_slots=False)`. Note that new attributes won't be part of `_fields` or `_asdict()`. affects: All versions of `namedlist`
Errors
Common errors & fixes
assert b.x == [4] # When 'x' was initialized with a default empty list in A
The default mutable object (e.g., a list) for a field was shared across all instances, meaning modifications to one instance's default affected others.
fixWrap mutable default values with `FACTORY` when defining the `namedlist`. Example: `A = namedlist('A', [('x', FACTORY(list))])` instead of `A = namedlist('A', [('x', [])])`. AttributeError: 'Point' object has no attribute 'z' (when trying to assign p.z = 2)
`namedlist` uses `__slots__` by default, which restricts instance attributes to only the defined fields. Attempts to add new attributes after creation will fail.
fixIf you require the ability to add arbitrary new attributes to instances, define the `namedlist` with `use_slots=False`: `Point = namedlist('Point', 'x y', use_slots=False)`. Upgrade
Version history
1.8latest on PyPI · released Aug 30, 2020
Audit
Dependencies
No dependency data recorded yet.