The Python Oracle

Which is better in python, del or delattr?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Island

--

Chapters
00:00 Which Is Better In Python, Del Or Delattr?
00:31 Accepted Answer Score 334
01:27 Answer 2 Score 50
01:58 Answer 3 Score 20
02:17 Answer 4 Score 16
02:34 Answer 5 Score 6
02:58 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 +