0
0
PythonConceptBeginner · 3 min read

__get__, __set__, __delete__ in Python: How They Work and When to Use

__get__, __set__, and __delete__ are special methods in Python used in descriptors to control how attributes are accessed, assigned, and deleted on objects. They let you customize what happens when you get, set, or delete an attribute, acting like a smart gatekeeper for attribute management.
⚙️

How It Works

Imagine you have a box with a lock, and you want to control who can open it, put things inside, or take things out. In Python, __get__, __set__, and __delete__ act like the lock and keys for an attribute inside a class. They let you decide what happens when someone tries to read, change, or remove that attribute.

These methods are part of a feature called descriptors. When you use a descriptor, Python calls these methods automatically whenever you access, assign, or delete the attribute. This way, you can add extra checks, log actions, or change the behavior without changing how the attribute looks from outside.

Think of it like having a smart assistant who watches over your stuff and makes sure everything is done correctly when you interact with it.

💻

Example

This example shows a descriptor that controls access to an attribute called value. It prints messages when you get, set, or delete the attribute.

python
class Descriptor:
    def __init__(self):
        self._value = None

    def __get__(self, instance, owner):
        print('Getting value')
        return self._value

    def __set__(self, instance, value):
        print('Setting value to', value)
        self._value = value

    def __delete__(self, instance):
        print('Deleting value')
        self._value = None

class MyClass:
    attr = Descriptor()

obj = MyClass()
obj.attr = 10
print(obj.attr)
del obj.attr
print(obj.attr)
Output
Setting value to 10 Getting value 10 Deleting value Getting value None
🎯

When to Use

Use __get__, __set__, and __delete__ when you want to control how attributes behave beyond simple storage. For example:

  • Validating data before saving it to an attribute.
  • Automatically updating related values when an attribute changes.
  • Logging or debugging attribute access.
  • Implementing computed properties that calculate values on the fly.

Descriptors are especially useful in frameworks and libraries where you want to add smart behavior to attributes without changing how users interact with them.

Key Points

  • __get__ runs when you read an attribute.
  • __set__ runs when you assign a value to an attribute.
  • __delete__ runs when you delete an attribute.
  • These methods are part of the descriptor protocol in Python.
  • Descriptors let you add custom behavior to attribute access.

Key Takeaways

__get__, __set__, and __delete__ let you control attribute access in Python classes.
They are used in descriptors to customize what happens when attributes are read, written, or deleted.
Use them to add validation, logging, or computed properties without changing how attributes are accessed.
Descriptors act like smart controllers for attributes, improving code flexibility and safety.