tags:

views:

1494

answers:

7

This may be silly, but its been nagging the back of my brain for a while.

So Python gives us two built-in ways to delete attributes from objects, the del command word and the delattr built-in function. I prefer delattr because it I think its a bit more explicit:

del foo.bar
delattr(foo, "bar")

But I'm wondering if there might be under-the-hood differences between them.

+3  A: 

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.

Jason Baker
+3  A: 

Unquestiably 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 :)

Alex Gaynor
I'm sure there's at least *one* person out there that would prefer getattr(foo, "bar") over foo.bar. Granted, I wouldn't agree with them. But that one person is still enough to make it not unquestionably the former.
Jason Baker
+15  A: 

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]

Miles
What tool did you use to generate this?
Triptych
The `dis` module. You can run it from the command line using `python -m dis` and typing in some code, or disassemble a function with `dis.dis()`.
Miles
Premature optimization is the root of all evil. ;-) But yes, you are right, of course.
Lennart Regebro
..so does it follow that the love of money is premature optimization?
John Fouhy
A: 

If you think delattr is more explicit, then why not used getattr all the time rather than object.attr?

As for under the hood... your guess is as good as mine. If not significantly better.

A: 

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 +

Algorias
A: 

Not sure about the inner workings, but from a code reusability and don't be a jerk coworker perspective, use del. It's more clear and understood by people coming from other languages as well.

+1  A: 

It's the same thing, they both end up calling the objects __delattr__ method. delattr is for when the attribute name is in a variable: delattr(theob, foo). Otherwise, using delattr is like crossing the stream for water.

In short: Use del.

Lennart Regebro