Built-in Classes Enhancement

Libraries for enhancing Python built-in classes.

Skip hand-written dunder methods, and when you compare Python dataclass alternatives, pick attrs for its validators and converters. Box gives dicts dot access.

How to choose:

  • Classes with generated dunder methods, validators, and converters: attrs
  • Dicts read with dot notation, nested ones included: python-box
  • Looking up a key by its value, with both directions in sync: bidict
  • A faster drop-in for the uuid module: uuid-utils

Listed in editorial order. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags
Replacement for __init__, __eq__, __repr__, etc. boilerplate in class definitions.
python-attrs/github.com/python-attrs/attrs /5,864 stars /
Efficient, Pythonic bidirectional map data structures and related functionality.
jab/github.com/jab/bidict /1,589 stars /
A fast, Rust-backed drop-in replacement for Python's built-in uuid module.
Python dictionaries with advanced dot notation access.
cdgriffith/github.com/cdgriffith/Box /2,841 stars /

Built-in Classes Enhancement guide

attrs writes the dunder methods that implement object protocols, so you don't have to. For new code, its docs recommend the modern API: @define on the class and field() for an attribute, or @frozen for immutable instances, with slots on by default. Type annotations stay optional. The standard library's dataclasses gave up features for simplicity, like validators and converters, so a class that needs them goes to attrs. It isn't a full serialization library, though: to serialize and validate your attrs classes, its docs point you to its sibling project, cattrs.

python-box installs Box, a dict subclass whose keys you can also read as attributes, even ones like "imdb stars". Nested dicts and lists become Box and BoxList objects, so dot access works all the way down: movie_box.Robin_Hood_Men_in_Tights.imdb_stars. It reads and writes JSON, YAML, TOML, and msgpack with methods like from_json() and to_json(), and to_dict() gives you plain dicts back. Keep Box for data whose keys change: attrs' docs say a dict with a fixed and known set of keys is an object, not a hash, and belongs in a class.

bidict gives you bidirectional mappings that work like dicts: look up a value by its key as usual, or a key by its value through .inverse, which stays in sync as you update the mapping. A single dict holding both directions mixes keys with values. Modeling the mapping correctly takes two one-way mappings kept in sync, which is what bidict does under the hood.

uuid-utils is a fast, drop-in replacement for Python's uuid module, powered by Rust. Import it under the module's name, import uuid_utils as uuid, and call uuid.uuid4() or uuid.uuid7() as usual. Django and some other frameworks require the standard library's own uuid.UUID instances. For those, import uuid_utils.compat instead, which returns them and still outperforms the standard library.

Know a project that belongs here?

Tell us what it does and why it stands out.