weakref rather than str(class_instance) due to id reuse
- 主要語言
- Python
- 星號
- 1.4k
- 分支
- 181
- PR 合併指標
- 30 天內沒有已合併 PR
描述
See [this on SO](https://stackoverflow.com/a/52181584) and links within, or a simple test case as follows:
```python
class Thing():
pass
thing_strs = set()
for idx in range(100):
thing = Thing()
print(f"{idx}: {str(thing)}")
assert str(thing) not in thing_strs, f"{str(thing)} already used (idx={idx})"
thing_strs.add(str(thing))
```
This prints:
```
0: <__main__.Thing object at 0x7fdfe8f74f90>
1: <__main__.Thing object at 0x7fdfe8f74fd0>
2: <__main__.Thing object at 0x7fdfe8f74f90>
Traceback (most recent call last):
File "", line 8, in
AssertionError: <__main__.Thing object at 0x7fdfe8f74f90> already used (idx=2)
```
This means that the cache is not scoped properly. It sounds like using [weakref](https://docs.python.org/3/library/weakref.html) is the better option if possible.
[This](https://github.com/aio-libs/aiocache/blob/master/aiocache/decorators.py#L142) is where it is used currently. Note that this actually applies to all references in the args/kwargs, not just the `self` object.
貢獻指南
評估
這個 Issue 還沒有評估資料。