Both x = None and del x look like ways to release a variable in Python, but they aren't equivalent. x = None keeps the name bound and just rebinds it to NoneType (still ~16 bytes); del x removes the binding entirely so the name itself is gone. Touch x after del and you get a NameError.
The full post walks through a worked example with sys.getsizeof and gc.collect() showing the byte difference, then disassembles both versions with dis.dis so you can see del compiles to a single DELETE_FAST while assigning to None becomes a LOAD_CONST + STORE_FAST pair.
Originally published at andreasbergstrom.dev — read the full post there.
Top comments (0)