Monkey patching a @property

后端 未结 7 1893
慢半拍i
慢半拍i 2021-02-03 18:33

Is it at all possible to monkey patch the value of a @property of an instance of a class that I do not control?

class Foo:
    @property
    def bar         


        
7条回答
  •  不知归路
    2021-02-03 18:54

    You can also patch property setters. Using @fralau 's answer:

    from module import ClassToPatch
    
    def foo(self, new_foo):
        self._foo = new_foo
    
    ClassToPatch.foo = ClassToPatch.foo.setter(foo)
    

    reference

提交回复
热议问题