TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
del statement is a built-in Python keyword used to remove bindings between names (references) and objects within a specific namespace, or to remove items and attributes from mutable objects. It does not directly delete objects from memory; rather, it removes the reference to the object, which decrements the object’s reference count. Actual memory deallocation is handled subsequently by Python’s garbage collector when an object’s reference count reaches zero.
Syntax
The formal syntax for thedel statement accepts a comma-separated list of targets:
Target Mechanics
The behavior of thedel statement changes depending on the nature of the target being evaluated.
1. Identifier (Name) Deletion
When the target is an identifier,del removes the binding of that name from the local or global namespace. Attempting to access the name after deletion raises a NameError.
global statement first.
Conversely, attempting to delete a variable explicitly declared as nonlocal is strictly prohibited. Python’s scoping rules do not allow removing bindings from an enclosing closure scope via del, and doing so will raise a SyntaxError (SyntaxError: cannot delete nonlocal).
2. Subscription and Slicing Deletion
When the target is a subscription (an index or dictionary key) or a slice, thedel statement delegates the operation to the container object by invoking its __delitem__(key) magic method.
3. Attribute Deletion
When the target is an attribute reference,del delegates the operation to the parent object by invoking its __delattr__(name) magic method. This removes the attribute from the object’s internal __dict__ (or __slots__).
Recursive Target Unpacking
Thetarget_list can contain nested tuples or lists. The del statement unpacks these recursively, deleting each bound target from left to right.
Restrictions
- You cannot delete literal values, expressions, or the results of function calls.
delstrictly requires a valid reference target. - Attempting to delete a subscription or slice of an immutable sequence (e.g.,
del my_tuple[0]) will raise aTypeError, as immutable types do not implement__delitem__. However, deleting the identifier bound to the immutable sequence itself (e.g.,del my_tuple) is perfectly valid, as this operates on the namespace reference rather than the underlying object.
Master Python with Deep Grasping Methodology!Learn More





