python's pre-declared constants are kinda weird

57 points by seb


hongminhee

builtins.True and friends are a fossil of pre-3.0 history. True and False started out as ordinary, reassignable builtins under PEP 285, landing first in 2.2.1 as a bugfix-release backport and then formalized properly in 2.3. None got its reassignment blocked in 2.4. True and False stayed assignable through the rest of the 2.x line and only became real keywords in 3.0.

The builtins slot is just what's left of that old namespace, kept around mostly so getattr/dir/vars introspection still works on them. It also explains why setattr(builtins, 'True', 67) doesn't touch the literal: the keyword compiles to a constant baked into the bytecode at compile time, separate from whatever's sitting in the builtins namespace.