Special Method Protocols

__del__() — object lifecycle protocol

Finalization hook, not the del statement itself. Its timing is not guaranteed; use explicit cleanup or a context manager for resources. This example calls the idempotent hook directly to demonstrate its body, not garbage-collection timing.

Formal Syntax & Structure

python syntaxpython
def __del__(self):

Return Value

None

No meaningful result; finalizer return is ignored.

Working Example

Example Snippetpython
class Marker:    def __init__(self): self.closed = False    def __del__(self): self.closed = True
def demonstrate():    marker = Marker()    marker.__del__()    return marker.closedprint(demonstrate())
Python Reference