Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The 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 the del statement accepts a comma-separated list of targets:
del target_list
A target can be an identifier (variable name), an attribute reference, a subscription (index/key), or a slicing.

Target Mechanics

The behavior of the del 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.
x = 100
y = x

del x  # Removes the name 'x' from the namespace

# print(x) -> Raises NameError


# The integer object 100 persists in memory because 'y' still references it.
If the identifier is located in a local scope (like inside a function), it is removed from the local namespace. To delete a global variable from within a local scope, the name must be declared with the 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, the del statement delegates the operation to the container object by invoking its __delitem__(key) magic method.

# Subscription deletion (Lists)
sequence = [10, 20, 30, 40]
del sequence[1]  # Invokes sequence.__delitem__(1)


# Slicing deletion
del sequence[1:3] # Invokes sequence.__delitem__(slice(1, 3))


# Subscription deletion (Dictionaries)
mapping = {'key1': 'value1', 'key2': 'value2'}
del mapping['key1'] # Invokes mapping.__delitem__('key1')

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__).
class Configuration:
    def __init__(self):
        self.host = "localhost"
        self.port = 8080

config = Configuration()
del config.port  # Invokes config.__delattr__('port')

Recursive Target Unpacking

The target_list can contain nested tuples or lists. The del statement unpacks these recursively, deleting each bound target from left to right.
a, b, c = 1, 2, 3


# The following statement deletes 'a' and 'b', but leaves 'c' intact.
del (a, b) 


# Equivalent to:

# del a

# del b

Restrictions

  • You cannot delete literal values, expressions, or the results of function calls. del strictly requires a valid reference target.
  • Attempting to delete a subscription or slice of an immutable sequence (e.g., del my_tuple[0]) will raise a TypeError, 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