Monkey patching a @property

后端 未结 7 1890
慢半拍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 19:04

    In case someone needs to patch a property while being able to call the original implementation, here is an example:

    @property
    def _cursor_args(self, __orig=mongoengine.queryset.base.BaseQuerySet._cursor_args):
        # TODO: remove this hack when we upgrade MongoEngine
        # https://github.com/MongoEngine/mongoengine/pull/2160
        cursor_args = __orig.__get__(self)
        if self._timeout:
            cursor_args.pop("no_cursor_timeout", None)
        return cursor_args
    
    
    mongoengine.queryset.base.BaseQuerySet._cursor_args = _cursor_args
    
    0 讨论(0)
提交回复
热议问题