Which is better in python, del or delattr?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 Question
00:37 Accepted answer (Score 326)
01:33 Answer 2 (Score 50)
02:07 Answer 3 (Score 19)
02:28 Answer 4 (Score 16)
02:46 Thank you
--
Full question
https://stackoverflow.com/questions/1120...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #del
#avk47
ACCEPTED ANSWER
Score 339
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]
ANSWER 2
Score 21
Unquestionably the former. In my view this is like asking whether foo.bar is better than getattr(foo, "bar"), and I don't think anyone is asking that question :)
ANSWER 3
Score 17
It's really a matter of preference, but the first is probably preferable. I'd only use the second one if you don't know the name of the attribute that you're deleting ahead of time.
ANSWER 4
Score 6
Just like getattr and setattr, delattr should only be used when the attribute name is unknown.
In that sense, it's roughly equivalent to several python features that are used to access built-in functionality at a lower level than you normally have available, such as __import__ instead of import and operator.add instead of +