[Python] Python descriptors for classes?

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    We know we have protocol descriptors like __get__ and __getattr__ that can be used on class instances. Now is there a descriptor that is called on when accessing class attributes?

    Say we have the following class:

    class Example:
    x = 1 # class attribute

    def __getattr__(self):
    return 2

    # Example().x == 2
    # Example.x == 1


    What descriptor can I use to make Example.x return 2 ?

    Continue reading...

Compartilhe esta Página