Registry / serialization / namedlist

namedlist

JSON →
library1.8pypypi✓ verified 84d ago

namedlist is a Python library that provides a factory function to create list-like objects with named fields, similar to `collections.namedtuple`, but with mutable instances. It also offers support for per-field default values and an optional default value for all fields. The current version is 1.8, released in August 2020. This package is no longer actively maintained, and the built-in `dataclasses` module is recommended for new projects.

pip install namedlist
INSTALL
IMPORT
SIG · NAMEDLIST
N
namedlist
serializationpythonv1.8
Install
1.5s avg
Import
9ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibc
py 3.103.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.
fix
Migrate 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.
fix
Use 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.
fix
If 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.
fix
Wrap 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.
fix
If 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.

Agent activity
2 hits · last 30 days
node
2
Resources
namedlist — pip install namedlist · libregistry