Registry / serialization / cons
library0.4.7pypypi✓ verified 28d ago

The `cons` library for Python provides an implementation of Lisp/Scheme-like cons cells, aiming to emulate their semantics while integrating with Python's built-in sequence types like lists and tuples. It facilitates functional programming patterns by allowing the construction and deconstruction of pairs. The library is actively maintained, with its current version being 0.4.7, and receives updates that include dependency bumps and Python version compatibility improvements.

pip install cons
INSTALL
IMPORT
SIG · CONS
C
cons
serializationpythonv0.4.7
Install
1.7s avg
Import
127ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.4.7 · 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.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.134s · 18.7MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.120s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cons
✓ from cons import cons
car
✓ from cons import car
cdr
✓ from cons import cdr

Demonstrates how to create cons cells with various Python sequence types and how to extract their `car` (head) and `cdr` (tail) components. It also shows `None` being treated as an empty list (nil) for consing.

from cons import cons, car, cdr # Create cons cells with different sequence types my_list_cons = cons(1, [2, 3]) my_tuple_cons = cons('a', ('b', 'c')) my_none_cons = cons(42, None) # None acts like 'nil' in some Lisps print(f"List cons: {my_list_cons}") print(f"Tuple cons: {my_tuple_cons}") print(f"None cons: {my_none_cons}") # Accessing the head (car) and tail (cdr) of a cons cell print(f"Car of list cons: {car(my_list_cons)}") # Output: 1 print(f"Cdr of list cons: {cdr(my_list_cons)}") # Output: [2, 3] # cons also works with dictionaries or other iterables from collections import OrderedDict my_ordereddict_cons = cons(('key', 'value'), OrderedDict()) print(f"OrderedDict cons: {my_ordereddict_cons}")
Debug
Known issues
breakingPython 3.7 support was deprecated in version 0.4.6. Users on Python 3.7 or older must upgrade their Python interpreter to at least 3.9 to use newer versions of `cons`.
fix
Upgrade Python environment to version 3.9 or higher.
affects: >=0.4.6
gotchaAttempting to call `car()` or `cdr()` on an empty Python sequence (e.g., `[]`, `()`) will raise a `ConsError`, as these are not considered valid `cons` pairs within the library's Scheme-like semantics.
fix
Always ensure the argument to `car()` or `cdr()` is a valid cons pair or a sequence that has been created with `cons()` and is not empty. Check for emptiness before deconstructing.
affects: All
gotchaBy default, `str` types are not treated as `cons`-pairs for `car`/`cdr` operations, even though they are Python sequences. Calling `cons("a", "string")` will produce a `ConsPair` object, but `car("string")` would raise `ConsError`.
fix
If `str` type consing and deconstruction is desired, the library's behavior for considering types as `Cons`-pairs can be overridden by registering classes with `MaybeCons` and `NonCons` from `cons.core`.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'cons'
The 'cons' library or its specific modules have not been installed or are not accessible in the current Python environment.
fix
Install the library using pip: `pip install cons`
AttributeError: module 'cons' has no attribute 'cons'
The user attempted to call `cons.cons()` (or `cons.car()`, `cons.cdr()`) after using `import cons`, but the functions `cons`, `car`, and `cdr` are intended to be imported directly from the package, not accessed as attributes of the `cons` module itself.
fix
Import the functions directly: `from cons import cons, car, cdr`
TypeError: 'int' object is not subscriptable
The `car` or `cdr` function was called with an argument that is not a sequence type (like a list or tuple) or a `cons` object, and the function internally attempted to access it using subscripting (e.g., `obj[0]`).
fix
Ensure that `car` and `cdr` are called with valid `cons` objects or Python sequences that represent a cons structure: `car(cons(1, []))` or `car([1, 2, 3])`.
TypeError: cons() missing 1 required positional argument: 'cdr'
The `cons` function was called with only one argument, but it requires two positional arguments: `car` (the first element) and `cdr` (the rest of the list/sequence or another cons cell).
fix
Provide both arguments to the `cons` function: `my_cons_cell = cons(1, [])` or `my_cons_cell = cons('a', ('b',))`
Upgrade
Version history
0.4.7latest on PyPI · released Jul 11, 2025
Audit
Dependencies
pythonrequiredRequires Python 3.9 or newer.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
cons — pip install cons · libregistry