Which is better in python, del or delattr?
The first is more efficient than the second. del foo.bar
compiles to two bytecode instructions:
2 0 LOAD_FAST 0 (foo) 3 DELETE_ATTR 0 (bar)
whereas delattr(foo, "bar")
takes five:
2 0 LOAD_GLOBAL 0 (delattr) 3 LOAD_FAST 0 (foo) 6 LOAD_CONST 1 ('bar') 9 CALL_FUNCTION 2 12 POP_TOP
This translates into the first running slightly faster (but it's not a huge difference – .15 μs on my machine).
Like the others have said, you should really only use the second form when the attribute that you're deleting is determined dynamically.
[Edited to show the bytecode instructions generated inside a function, where the compiler can use LOAD_FAST
and LOAD_GLOBAL
]
- del is more explicit and efficient;
- delattr allows dynamic attribute deleting.
Consider the following examples:
for name in ATTRIBUTES: delattr(obj, name)
or:
def _cleanup(self, name): """Do cleanup for an attribute""" value = getattr(self, name) self._pre_cleanup(name, value) delattr(self, name) self._post_cleanup(name, value)
You can't do it with del.