{"id":3938,"library":"cons","title":"Cons Python Library","description":"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.","status":"active","version":"0.4.7","language":"en","source_language":"en","source_url":"https://github.com/pythological/python-cons","tags":["data structures","lisp","scheme","functional programming","collections"],"install":[{"cmd":"pip install cons","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires Python 3.9 or newer.","package":"python","optional":false}],"imports":[{"symbol":"cons","correct":"from cons import cons"},{"symbol":"car","correct":"from cons import car"},{"symbol":"cdr","correct":"from cons import cdr"}],"quickstart":{"code":"from cons import cons, car, cdr\n\n# Create cons cells with different sequence types\nmy_list_cons = cons(1, [2, 3])\nmy_tuple_cons = cons('a', ('b', 'c'))\nmy_none_cons = cons(42, None) # None acts like 'nil' in some Lisps\n\nprint(f\"List cons: {my_list_cons}\")\nprint(f\"Tuple cons: {my_tuple_cons}\")\nprint(f\"None cons: {my_none_cons}\")\n\n# Accessing the head (car) and tail (cdr) of a cons cell\nprint(f\"Car of list cons: {car(my_list_cons)}\") # Output: 1\nprint(f\"Cdr of list cons: {cdr(my_list_cons)}\") # Output: [2, 3]\n\n# cons also works with dictionaries or other iterables\nfrom collections import OrderedDict\nmy_ordereddict_cons = cons(('key', 'value'), OrderedDict())\nprint(f\"OrderedDict cons: {my_ordereddict_cons}\")","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python environment to version 3.9 or higher.","message":"Python 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`.","severity":"breaking","affected_versions":">=0.4.6"},{"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.","message":"Attempting 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.","severity":"gotcha","affected_versions":"All"},{"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`.","message":"By 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`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}