[Python] Is it possible to overload Python assignment?

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a magic method that can overload the assignment operator, like __assign__(self, new_value)?

    I'd like to forbid a re-bind for an instance:

    class Protect():
    def __assign__(self, value):
    raise Exception("This is an ex-parrot")

    var = Protect() # once assigned...
    var = 1 # this should raise Exception()


    Is it possible? Is it insane? Should I be on medicine?

    Continue reading...

Compartilhe esta Página