What in Python you can lean on architecturally, what to use with a caveat, and what not to pin down even with a test.
One question separates knowing the language from knowing the implementation: will my code break on another implementation or in the next release? Below is a registry of every non-obvious point in the canon, sorted by that question. There is no need to read it in order: the page is meant as a reference to come back to.
In the registry: 25 guarantees · 25 details · 7 undefined
is compares identity, == compares value; identity is unique for the object's lifetimeid() must not be stored as a long-lived key — it will collide with someone else's.id() returns the object's address in memorydef executesa is b is true for themis.'ab' is 'ab' is true, while the same string assembled at runtime is already a different object.sys.intern carries a guarantee.int has arbitrary precision; // rounds down, and the sign of % matches the sign of the divisorint: an array of 30-bit digits, sys.getsizeof(2**70) == 36float is an IEEE 754 double with all its properties0.1 + 0.2 != 0.3 here for exactly the same reason as in C.list.append is O(1), list.insert(0, …) is O(n), x in list is O(n), x in set and x in dict are O(1)sys.getsizeof of a list is a way to write a fragile test.dict preserves insertion order (since 3.7)sethash('string') across process runsPYTHONHASHSEED) — protection against collision attacks. Hence: a hash must not be saved to disk.a == b implies hash(a) == hash(b), and the hash does not change during the object's lifetimedict layout, shared instance keys (PEP 412), values stored in the object's preambledict and set the same operation gives an explicit RuntimeError 🔒.__dict__ → the type's MRO → __getattr__property, classmethod and super() follows from it.super() follows the MRO of the instance, not the class's parent__getattr__ and the instance's __dict__type.__setattr__ in CPython, not a promise.__slots__ saves memory; the specific saving in bytes__dict__), the size is not: it depends on the version, because the instance layout has changed.open(…).read() without with). PyPy, GraalPy and Jython behave differently, and that is not their bug.__del__ is called at all__del__ — only on with or atexit.with calls __exit__ on any exit from the block, including an exception and return__exit__ that returns a true value suppresses the exceptioncontextlib.suppress.return or break in finally swallows an exception in flightiter(it) is it for any iteratoryield from passes send, throw and the return value straight throughfor x in gen: yield x does not do this. await stands on this difference.GeneratorExit and runs its finallywith.awaits a coroutine's code runs without preemptionx += 1, “check and write”, “read and update” are races despite the GIL. The right answer is always a lock or a queue.sys.setswitchintervalfork from a process that has threadsspawn by default.sys.modulessys.modules before its body executesfrom x import y creates a separate binding in the importing modulex: int in a class body does not create an attributedataclass and pydantic follows from someone reading that data.__annotations__abi3. Hence the wheel matrix and “the package will not install on the new Python”..pyc format, the magic number and the contents are not: a .pyc from another minor version is simply ignored.UnboundLocalError, global and nonlocal. This is the specification, not a detail.except ... as is deleted on leaving the blocktry. Done so that the traceback does not keep the frame alive. If you need the object later, assign it to another name inside the block..pyc by the source's modification time and sizetry without a raise costs nothingco_exceptiontable, which is read only on an exception. Before 3.11 entering the block executed an instruction, and benchmarks from those years show overhead.CALL pushes a frame and continues the same loop. That is why 20,000 frames of recursion do not bring the process down — but recursion through C still consumes the stack, and there the limit offers real protection.__del__ of a module-level global is called at exitatexit runs earlier and more reliably; os._exit lets nothing run.