gpt4 book ai didi

python - 在 python 中重写 __setattr__ 和 __getattribute__ 时,无法更新可变实例属性

转载 作者:太空宇宙 更新时间:2023-11-03 21:25:20 27 4
gpt4 key购买 nike

我继承了一堆遗留代码,并遇到了一些棘手的问题。我相信这里尝试的想法是使用容器化缓存(例如 redis)来保存要在多个进程之间共享的某些属性。除了这是否是完成任务的最佳方式之外,我偶然发现了一些让我困惑的事情。如果如下重写 __getattribute____setattr__ 方法,则无法更新可变实例属性。我希望对 self.b 所做的更改能够反射(reflect)在 print() 调用中,但正如您从输出中看到的那样,仅打印初始的空白字典。单步执行 self.b["foo"] = "bar" 行显示调用了 __getattribute__ 并从缓存中检索了正确的对象,但没有调用to __setattr__ 已创建,字典似乎也没有以任何方式更新。有人知道为什么会这样吗?

import dill

_reserved_fields = ["this_is_reserved"]

class Cache:
_cache = {}

def get(self, key):
try:
return self._cache[key]
except KeyError:
return None

def set(self, key, value):
self._cache[key] = value

def exists(self, key):
return key in self._cache


class Class(object):
def __init__(self, cache):
self.__dict__['_cache'] = cache
self.a = 1
self.a = 2
print(self.a)
self.b = dict()
self.b["foo"] = "bar" # these changes aren't set in the cache and seem to disappear
print(self.b)
self.this_is_reserved = dict()
self.this_is_reserved["reserved_field"] = "value" # this updates fine
print(self.this_is_reserved)

def __getattribute__(self, item):
try:
return object.__getattribute__(self, item)
except AttributeError:
key = "redis-key:" + item
if not object.__getattribute__(self, "_cache").exists(key):
raise AttributeError
else:
obj = object.__getattribute__(self, "_cache").get(key)
return dill.loads(obj)

def __setattr__(self, key, value):
if key in _reserved_fields:
self.__dict__[key] = value
elif key.startswith('__') and key.endswith('__'):
super().__setattr__(key, value)
else:
value = dill.dumps(value)
key = "redis-key:" + key
self._cache.set(key, value)

if __name__ == "__main__":
cache = Cache()
inst = Class(cache)

# output
# 2
# {}
# {'reserved_field': 'value'}

最佳答案

self.b["foo"] = "bar" 正在获取来自 self 的属性 b,其中 b 被假定为一个字典,并分配给该字典中的键 "foo"__setattr__ 仅当您直接分配给 b 时才会被调用,例如 self.b = {**self.b, 'foo': 'bar'} (在 Python 3.5+ 中用于字典更新)。

无需在存储的对象中添加 Hook 来触发对其 __setattr__ 及其子对象 __settattr__ 等的缓存保存,您可以添加一种方法显式触发缓存更新(意味着用户代码需要了解缓存),或者避免改变字段,除非通过直接赋值。

对你的运气感到抱歉;)。

关于python - 在 python 中重写 __setattr__ 和 __getattribute__ 时,无法更新可变实例属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com