4. Descriptor Protocols
These examples demonstrate how Python's descriptor protocol (__get__, __set__, and __set_name__) allows fine-grained control over attribute access and modification.
1. Basic Descriptor Example
class Descriptor:
def __get__(self, instance, owner):
print("Getting value")
return instance._value
def __set__(self, instance, value):
print("Setting value")
instance._value = value
class MyClass:
attribute = Descriptor()
obj = MyClass()
obj.attribute = 42 # Setting value
print(obj.attribute) # Getting value, Output: 422. Read-Only Descriptor
class ReadOnlyDescriptor:
def __get__(self, instance, owner):
return "This is a read-only attribute"
def __set__(self, instance, value):
raise AttributeError("Cannot modify read-only attribute")
class MyClass:
read_only = ReadOnlyDescriptor()
obj = MyClass()
print(obj.read_only) # Output: This is a read-only attribute
# obj.read_only = "New value" # Raises AttributeError3. Descriptor with Validation
4. Non-Data Descriptor
5. Descriptor in Class Variables
6. Combining __get__ and __set__
7. Tracking Access with a Descriptor
8. Descriptor to Control Multiple Attributes
9. Using a Descriptor for Logging
10. Descriptor with Property-Like Behavior
Last updated