Skip to content

Latest commit

 

History

History
22 lines (11 loc) · 1.2 KB

descriptors.md

File metadata and controls

22 lines (11 loc) · 1.2 KB

Descriptors

Descriptors are a powerful feature in Python that allow you to define how attributes are accessed and modified on instances of a class.

A descriptor is an object that defines one or more of the following methods:

  • get(self, instance, owner): Gets the value of the attribute from the instance of the class or its parent class.

  • set(self, instance, value): Sets the value of the attribute on the instance of the class or its parent class.

  • delete(self, instance): Deletes the attribute from the instance of the class or its parent class.

Descriptors are typically used to implement computed properties, type checking and validation, and other advanced behavior for attributes.

Here's an example of a descriptor that implements a read-only property:

![[Pasted image 20231205122217.png]]

In this example, ReadOnlyDescriptor is a descriptor that stores a value and returns it when the x attribute is accessed. Since ReadOnlyDescriptor only defines a get method and not a set method, the x attribute is read-only and attempting to set its value will raise an AttributeError

Visit https://www.youtube.com/watch?v=ovsvGtWD90Y