delExpert Examples

Deletes a variable, list item, dictionary entry, or object attribute

del bytecode

How del compiles for different targets.

python
import dis

def del_name():
    x = 1
    del x

def del_item():
    d = {}
    del d["key"]

def del_attr():
    class C: pass
    c = C()
    del c.x

print("del variable:")
dis.dis(del_name)
print("\ndel item:")
dis.dis(del_item)
print("\ndel attribute:")
dis.dis(del_attr)

# del x -> DELETE_FAST (local) or DELETE_NAME (global)
# del d[k] -> DELETE_SUBSCR
# del o.x -> DELETE_ATTR

del compiles to different opcodes depending on the target: DELETE_FAST/DELETE_NAME for variables, DELETE_SUBSCR for items, DELETE_ATTR for attributes. Each triggers different internal protocols.

Want to try these examples interactively?

Open Expert Playground