__set__Easy Examples

Descriptor protocol: called to set an attribute on the owner class

Implementing __set__

Basic implementation of __set__ in a class.

python
class Example:
    def __set__(self):
        return "Example __set__"

obj = Example()
print(obj)

__set__ descriptor protocol: called to set an attribute on the owner class. Implementing it lets you customize how Python interacts with your objects.

__set__ in action

Seeing __set__ called by Python's built-in operations.

python
# How Python calls __set__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __set__(self):
        print(f"__set__ was called!")
        return self

d = Demo(42)
# This triggers __set__:
print(d)

Python automatically calls __set__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground