Which is better in python, del or delattr?

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

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.

8 Answers
8

Leave a Comment