{"id":9133,"library":"namedlist","title":"namedlist","description":"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.","status":"maintenance","version":"1.8","language":"en","source_language":"en","source_url":"https://github.com/timgates42/namedlist","tags":["data structures","namedtuple","mutable","dataclasses alternative","list-like"],"install":[{"cmd":"pip install namedlist","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"namedlist","correct":"from namedlist import namedlist"},{"note":"FACTORY is used for mutable default values to prevent shared state.","symbol":"FACTORY","correct":"from namedlist import namedlist, FACTORY"}],"quickstart":{"code":"from namedlist import namedlist, FACTORY\n\n# Basic mutable namedlist\nPoint = namedlist('Point', 'x y')\np = Point(1, 3)\nprint(f\"Initial point: {p}\")\np.x = 2\nprint(f\"Modified point: {p}\")\nassert p.x == 2\nassert p.y == 3\n\n# Namedlist with default values\nRect = namedlist('Rect', 'x1 y1 x2 y2', default=0)\nr = Rect(x1=10, y1=10)\nprint(f\"Rectangle with defaults: {r}\")\nassert r.x1 == 10 and r.y1 == 10 and r.x2 == 0 and r.y2 == 0\n\n# Namedlist with a factory for mutable defaults (e.g., list)\nConfig = namedlist('Config', [('name', 'default'), ('options', FACTORY(list))])\nc1 = Config(name='App1')\nc1.options.append('verbose')\nprint(f\"Config 1: {c1}\")\n\nc2 = Config(name='App2') # new instance, new list for options\nprint(f\"Config 2: {c2}\")\nassert c1.options == ['verbose']\nassert c2.options == [] # Demonstrates independent mutable defaults\n","lang":"python","description":"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."},"warnings":[{"fix":"Migrate to `dataclasses`. For example, a `namedlist` can often be directly replaced by a `@dataclass`.","message":"The `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.","severity":"deprecated","affected_versions":"All versions, especially 1.8+"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions of `namedlist`"},{"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()`.","message":"By 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.","severity":"gotcha","affected_versions":"All versions of `namedlist`"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Wrap mutable default values with `FACTORY` when defining the `namedlist`. Example: `A = namedlist('A', [('x', FACTORY(list))])` instead of `A = namedlist('A', [('x', [])])`.","cause":"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.","error":"assert b.x == [4] # When 'x' was initialized with a default empty list in A"},{"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)`.","cause":"`namedlist` uses `__slots__` by default, which restricts instance attributes to only the defined fields. Attempts to add new attributes after creation will fail.","error":"AttributeError: 'Point' object has no attribute 'z' (when trying to assign p.z = 2)"}]}