Suppose someone didn't know what the difference was between __del__ and __delete__? Write an explanation.
What is the difference between `__del__` and `__delete__`?
7.9k Views Asked by Toothpick Anemone AtThere are 2 best solutions below
On
object.__del__(self):
Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a
__del__()method, the derived class’s__del__()method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance.
I think this means that my_object.__del__ will get called by CPython's garbage collector after the reference count for my_object drops to zero.
object.__delete__(self, instance):
Called to delete the attribute on an instance
instanceof the owner class.
The __delete__ dunder method is related to python's notion of descriptors; a descriptor is "any object which defines the methods __get__(), __set__(), or __delete__()." Descriptors can be used to implement custom behavior for attribute lookup/assignment/deletion (via __get__/__set__/__delete__, respectively).
See also:
- The
delstatement: "Deletion of a name removes the binding of that name from the local or global namespace... Deletion of attribute references, subscriptions and slicings is passed to the primary object involved..." - The
delattrbuilt-in function. From the docs, "The function deletes the named attribute, provided the object allows it. For example,delattr(x, 'foobar')is equivalent todel x.foobar." object.__delattr__(self, name)is called when attribute deletion is attempted. According to the docs, "This should only be implemented ifdel obj.nameis meaningful for the object." Thus, defining a user class with the methodMyClass.__delattr__enables custom behavior when e.g. the statementdel my_object.an_attris invoked, or (equivalently) whendelattr(my_object, 'an_attr')is called.object.__delitem__(self, key)is "Called to implement deletion ofself[key]." Thus, defining a user class with the methodMyClass.__delitem__enables custom behavior when e.g. the statementdel my_object[a_key]is invoked.
__del__is called when you delete an object and__delete__is sometimes called when you delete an attribute of an object.ABOUT
__del__:The following code shows when
__del__gets called:If
my_instanceis the last label pointing to the data, thendel my_instancecallsMyClass.__del__(my_instance)Technically,
del my_instanceonly deletes the labelmy_instance. Imagine people at a party all wearing names tags. Sometimes a person has 6 or 7 names tags on simultaneously, and other times, they only have one. Python will kick anyone out of the party who is not wearing at least one name tag.MyClass.__del__(my_instance)gets called when the last name-tag/label is removed from a piece of data.The code above shows an example of when we make sure to close an open file. Another example might be to count of the number active instances of a given class:
ABOUT
__delete__Unlike
__del__,__delete__has to do with descriptors. The code below describes the behavior ofobj.my_varorgetattr(obj, “my_var”)class Klaus: def getattribute(self, attrname): try: attribute = attrname from instance Klaus except AttributeError: attribute = attrname from class Klaus
If
my_varis a descriptor, then following two lines of code equivalent:Just as
__getattribute__checks whether the attribute has a__get__method or not,__delattr__will check whether the attribute has a__delete__method or not.You can see when
__delete__gets called by viewing the following code:When dealing with descriptors, the following lines of code are all equivalent: