Lesser known parts of Python standard library
Collections & data structures
collectionshighlighted as especially valuable:defaultdict,OrderedDict,namedtuple,deque,ChainMap, andCounter.defaultdictpraised for nested-structure accumulation without manual initialization.ChainMapnoted as underrated: layered configs, combined namespaces, structured logging, and storage abstraction.frozensetcited for hashable/immutable set semantics, especially for combination-based indexing where order shouldn’t matter.arrayandstructmentioned for memory‑efficient homogeneous data and binary packing/unpacking; arrays are often slower than lists but use far less memory.
Itertools, functools & functional style
itertools(e.g.,takewhile,cycle,chain,groupby) andfunctools(lru_cache,partial,wraps) broadly praised as “superpowers.”- Some complain Python’s syntax and weak typing support make complex iterator pipelines unwieldy, leading them to other languages (e.g., Kotlin) or to converting back to lists.
more_itertools,toolz, and the itertools “recipes” are mentioned as valuable complements.
Dict ordering & OrderedDict debate
- Extensive debate: since 3.7,
dict’s insertion order is guaranteed by the language spec (3.6 had it as a CPython detail). - Many argue
OrderedDictis mostly obsolete except for extra methods (move_to_end, queue/stack patterns, order-sensitive equality). - Others still use it for older Python (2.x/3.6), readability (“order matters here”), or to avoid relying on version-specific guarantees.
- Some warn that treating pre‑3.7 dicts as ordered has caused rare, hard-to-debug bugs.
Dataclasses, namedtuple & validation libs
- Several people have moved from
namedtupleto frozendataclasses; others still likenamedtuplefor light weight and helpers like_make()/_asdict(). - Example patterns: mapping DB rows into named tuples or dataclasses; using
sqlite3.Rowas an alternative. - attrs/cattrs preferred by some over Pydantic, which is seen as ergonomically heavy.
- Pydantic raised as a “next step” but met with pushback over friction.
Other lesser-known stdlib features
graphlib.TopologicalSorterappreciated but criticized for a rigid interface; some say topo sort is easy enough to hand-roll.MappingProxyTypepraised for read‑only live dict views.statisticsliked for convenience but acknowledged as ~10x slower than NumPy.decimalandfractions.Fractionmentioned; relation to IEEE decimal arithmetic marked as unclear.- Other callouts:
contextlib.ExitStack(flattening context managers),disvsastfor inspection,graphlib,Fractionsyntax sugar,zipapp.
CLIs, built-ins & “Easter eggs”
- Handy CLI modules:
python -m http.server,-m json.tool,-m zipfile, pluspydoc -p 0and in‑REPLhelp(). - Lightweight HTTP server in stdlib considered “good enough” for simple cases; some wish for a Flask‑like server and a
requests-style client in stdlib. - Humorous/educational imports:
antigravity,from __future__ import braces,import this.
LLMs vs batteries-included
- One line of discussion suggests LLMs lower the need for tiny helper functions in stdlib or third‑party libs: developers can generate small utilities on demand.
- Others argue this is a step backward versus using well‑tested, idiomatic stdlib tools; homegrown/LLM‑generated code risks subtle bugs and inconsistency.
- There’s recognition that past debates about adding utilities vs. relying on libraries (e.g.,
more_itertools) might be softened by cheap code generation, but concerns remain about readability and maintainability.