The Python Language Reference, Release 3.9.5 27 3.3. Special method names It is not guaranteed that$_{__del__()}$ methods are called for objects that still exist when the interpreter exits. Note: $_{del x}$doesn’t directly call$_{x.__del__()}$ — the former decrements the reference count for$_{x}$ by one, and the latter is only called when x ’s reference count reaches zero. CPython implementation detail: It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the cyclic garbage collector . A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback. See also: Documentation for the$_{gc}$module. Warning: Due to the precarious circumstances under which$_{__del__()}$ methods are invoked, excep- tions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. In particular: object. __repr__ ( self$_{)}$ Called by the$_{repr()}$ built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form$_{<...some}$ useful description...> should be returned. The return value must be a string object. If a class defines$_{__repr__()}$ but not$_{__str__()}$ , then$_{__repr__()}$ is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unam- biguous. The default implementation defined by the built-in type$_{object}$calls$_{object.__repr__()}$ . This method differs from$_{object.__repr__()}$ in that there is no expectation that$_{__str__()}$ return a valid Python expression: a more convenient or concise representation can be used. object. __str__ ( self$_{)}$ Called by$_{str(object)}$ and the built-in functions$_{format()}$ and$_{print()}$ to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. object. __bytes__ ( self$_{)}$ Called by bytes to compute a byte-string representation of an object. This should return a$_{bytes}$object. object. __format__ ( self , format_spec$_{)}$ Called by the$_{format()}$ built-in function, and by extension, evaluation of formatted string literals and the str.format() method, to produce a “formatted” string representation of an object. The format_spec ar- gument is a string that contains a description of the formatting options desired. The interpretation of the format_spec argument is up to the type implementing$_{__format__()}$ , however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax. See formatspec for a description of the standard formatting syntax. The return value must be a string object. • __del__() can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to$_{None}$ . Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the$_{__del__()}$ method is called. • __del__() can be invoked when arbitrary code is being executed, including from any arbitrary thread. If __del__() needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute$_{__del__()}$ .