The Python Oracle

Which is better in python, del or delattr?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Which Is Better In Python, Del Or Delattr?
00:22 Answer 1 Score 17
00:37 Answer 2 Score 21
00:53 Accepted Answer Score 339
01:36 Answer 4 Score 6
02:01 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 +