what are Descriptors & Dunder Protocols

 In Python, both Descriptors and Dunder Protocols are advanced concepts that allow you to customize how objects behave, particularly concerning attribute access and common operations. They are fundamental to how Python's object model works under the hood.

Descriptors

A descriptor is an object attribute that has "binding behavior," meaning its attribute access (getting, setting, or deleting) has been overridden by methods in the descriptor protocol. In simpler terms, a descriptor is a class that, when its instances are used as attributes in another class, defines custom logic for how those attributes are interacted with.

Key characteristics of descriptors:

  • Implement a protocol: A class becomes a descriptor if it implements one or more of the following "dunder" methods:

    • __get__(self, instance, owner): Called when the attribute is accessed (e.g., obj.attribute).

      • self: The descriptor instance itself.

      • instance: The instance of the class that the descriptor is attached to (or None if accessed via the class itself, like ClassName.attribute).

      • owner: The class that the descriptor is attached to (ClassName).

    • __set__(self, instance, value): Called when the attribute is assigned a value (e.g., obj.attribute = value).

      • self: The descriptor instance itself.

      • instance: The instance of the class that the descriptor is attached to.Python Training in Bangalore

      • value: The value being assigned to the attribute.

    • __delete__(self, instance): Called when the attribute is deleted (e.g., del obj.attribute).

      • self: The descriptor instance itself.

      • instance: The instance of the class that the descriptor is attached to.

    • __set_name__(self, owner, name) (since Python 3.6): Called when the descriptor is assigned to an attribute in the owner class. This allows the descriptor to know the name of the attribute it's managing.

  • Types of Descriptors:

    • Data Descriptor: If a descriptor implements __set__ or __delete__ (in addition to or instead of __get__), it's a data descriptor. Data descriptors have higher precedence than instance dictionaries; meaning, if an instance has an attribute with the same name as a data descriptor, the descriptor's __get__ (or __set__, __delete__) will still be called.

    • Non-Data Descriptor: If a descriptor implements only __get__, it's a non-data descriptor. Non-data descriptors can be overridden by instance attributes. If an instance has an attribute with the same name, that instance attribute will take precedence.

Why use descriptors?

Descriptors are powerful for:

  • Attribute Validation: Ensuring that an attribute's value always meets certain criteria (e.g., a number is positive, a string is a valid email).

  • Managed Attributes (Properties): The built-in property() function and @property decorator are implemented using descriptors, allowing you to define getter, setter, and deleter methods for an attribute while still accessing it with simple dot notation.Best Python Training in Bangalore

  • Lazy Loading: Computing attribute values only when they are accessed for the first time.

  • Logging Access: Tracking when an attribute is read, modified, or deleted.

  • Creating built-in behaviors: staticmethod, classmethod, and even methods themselves are implemented using descriptors.

Conclusion

In 2025,Python will be more important than ever for advancing careers across many different industries. As we've seen, there are several exciting career paths you can take with Python , each providing unique ways to work with data and drive impactful decisions., At Nearlearn is the Top Python Training in Bangalore  we understand the power of data and are dedicated to providing top-notch training solutions that empower professionals to harness this power effectively. One of the most transformative tools we train individuals on is Python.






Comments

Popular posts from this blog

Debugging in python

"How Does Python Aid in Social Media Analytics?"

What Are the Top 10 Python Libraries for Data Science