GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python __delete__ Method - Complete Guide Python __delete__ MethodLast modified April 8, 2025
This comprehensive guide explores Python's Basic Definitions
The
Key characteristics: it's defined in a descriptor class, takes the instance as
an argument (conventionally named Basic Descriptor with __delete__
Here's a simple descriptor implementation showing basic_delete.py
class LoggedDelete:
def __get__(self, instance, owner):
print("Getting value")
return instance._value
def __set__(self, instance, value):
print("Setting value")
instance._value = value
def __delete__(self, instance):
print("Deleting value")
del instance._value
class MyClass:
attr = LoggedDelete()
obj = MyClass()
obj.attr = 42 # Calls __set__
val = obj.attr # Calls __get__
del obj.attr # Calls __delete__
This example demonstrates the full descriptor protocol. When we delete
The Preventing Attribute Deletion
prevent_delete.py
class ImmutableAttribute:
def __get__(self, instance, owner):
return instance._protected_value
def __set__(self, instance, value):
instance._protected_value = value
def __delete__(self, instance):
raise AttributeError("Cannot delete immutable attribute")
class Config:
setting = ImmutableAttribute()
config = Config()
config.setting = "production"
print(config.setting)
# del config.setting # Raises AttributeError
This implementation makes an attribute effectively immutable after creation.
Attempting to delete
The actual value is stored in Property-like Deletion Control
The property_delete.py
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def celsius(self):
return self._celsius
@celsius.setter
def celsius(self, value):
self._celsius = value
@celsius.deleter
def celsius(self):
print("Resetting temperature")
self._celsius = 0
temp = Temperature(25)
print(temp.celsius) # 25
del temp.celsius # Calls deleter
print(temp.celsius) # 0
This example shows the property-based approach to attribute deletion control.
The Instead of completely removing the attribute, this implementation resets the temperature to 0 when deletion is attempted. Database-backed Attribute Deletion
database_delete.py
class DatabaseField:
def __init__(self, field_name):
self.field_name = field_name
def __get__(self, instance, owner):
# In a real implementation, this would query the database
return instance._data.get(self.field_name)
def __set__(self, instance, value):
# This would update the database
instance._data[self.field_name] = value
def __delete__(self, instance):
# This would delete from the database
print(f"Deleting {self.field_name} from database")
instance._data.pop(self.field_name, None)
class User:
name = DatabaseField('username')
def __init__(self):
self._data = {} # Simulating database storage
user = User()
user.name = "Alice"
print(user.name) # Alice
del user.name # Calls __delete__
print(user.name) # None
This simplified example shows how
In a real implementation, Lazy Resource Cleanup
resource_delete.py
class ManagedFile:
def __get__(self, instance, owner):
if instance is None:
return self
return open(self.filename, 'r').read()
def __set__(self, instance, value):
with open(self.filename, 'w') as f:
f.write(value)
def __delete__(self, instance):
print(f"Cleaning up {self.filename}")
try:
import os
os.remove(self.filename)
except FileNotFoundError:
pass
class Document:
content = ManagedFile()
def __init__(self, filename):
self.filename = filename
doc = Document("test.txt")
doc.content = "Hello World"
print(doc.content)
del doc.content # Deletes the file
This example shows
The Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |