Dynamically modifying "__setattr__"

Liste des GroupesRevenir à cl python 
Sujet : Dynamically modifying "__setattr__"
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 28. Sep 2023, 10:44:01
Autres entêtes
Organisation : Stefan Ram
Message-ID : <setattr-20230928104008@ram.dialup.fu-berlin.de>
  I wanted to postpone the introduction of a custom __setattr__
  to a point later at runtime where I then wanted to use
  "self.__setattr__ = self.setattr" to set this attribute. But this
  did not have the effect desired to print "setattr called" as can
  be seen below.

  main.py

class A:
    def __init__( self ):
        self.__setattr__ = self.setattr
    def setattr( self, key, value ):
        print( 'setattr called.' )

a = A()
a.x = 1

  transcript

  Any idea how to achieve something like this?



Date Sujet#  Auteur
28 Sep 23 * Dynamically modifying "__setattr__"2Stefan Ram
30 Sep 23 `- Re: Dynamically modifying "__setattr__"1Stefan Ram

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal