{"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":"python","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,"search_vec":"'0.4.7':58 'activ':51 'aim':16 'allow':41 'built':27 'built-in':26 'bump':65 'cell':15 'collect':77 'compat':69 'con':1,5,14 'construct':43 'current':55 'data':71 'deconstruct':45 'depend':64 'emul':18 'facilit':36 'function':37,75 'implement':11 'improv':70 'includ':63 'integr':22 'librari':3,6,49 'like':31 'lisp':73 'lisp/scheme-like':13 'list':32 'maintain':52 'pair':47 'pattern':39 'program':38,76 'provid':9 'python':2,8,24,67 'receiv':60 'scheme':74 'semant':20 'sequenc':29 'structur':72 'tupl':34 'type':30 'updat':61 'version':56,68","created_at":"2026-04-12T03:34:04.178469+00:00","updated_at":"2026-04-16T03:38:39.320254+00:00","problems":[{"fix":"Install the library using pip: `pip install cons`","cause":"The 'cons' library or its specific modules have not been installed or are not accessible in the current Python environment.","error":"ModuleNotFoundError: No module named 'cons'"},{"fix":"Import the functions directly: `from cons import cons, car, cdr`","cause":"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.","error":"AttributeError: module 'cons' has no attribute 'cons'"},{"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])`.","cause":"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]`).","error":"TypeError: 'int' object is not subscriptable"},{"fix":"Provide both arguments to the `cons` function: `my_cons_cell = cons(1, [])` or `my_cons_cell = cons('a', ('b',))`","cause":"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).","error":"TypeError: cons() missing 1 required positional argument: 'cdr'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.4.7","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/pythological/python-cons","docs":null,"changelog":null,"pypi":"https://pypi.org/project/cons/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}