Explain Python's descriptor protocol and its applications.
Answer
Descriptors are objects with __get__, __set__, and/or __delete__ methods, controlling attribute access. Data descriptors: define __set__ or __delete__. Non-data descriptors: only __get__. Lookup order: data descriptors > instance dict > non-data descriptors > class dict. Applications: @property (descriptor returning function result), @classmethod/@staticmethod, Django ORM fields, lazy attributes, type checking, logging attribute access. Example: class ValidatedAttribute with __set__ validating values. Descriptors power Python's object system - methods are non-data descriptors binding functions to instances.
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.