Message416231
Jelle Zijlstra:
> I believe the attrs code wouldn't work if a method is decorated with a decorator that wraps the original function, such as @functools.cache.
What do you mean by "wouldn't work"? Do you mean that the semantics of "copy_class()" should be better defined? Currently, copy.deepcopy(MyClass) doesn't copy the class at all, it returns the class unmodified :-)
@functools.cache is designed for unbound methods.
Example:
---
import attr
import functools
@attr.s(slots=True)
class A:
@staticmethod
@functools.cache
def incr(x):
return x + 1
@staticmethod
@functools.lru_cache
def incr_lru(x):
return x + 1
obj = A()
print(obj.incr(1))
print(obj.incr_lru(2))
---
Output (current Python main branch, attrs 21.4.0):
---
2
3
---
@attr.s(slots=True) copies a class but then drops the original class. It doesn't create two classes which share methods, functools caches, etc. |
|
Date |
User |
Action |
Args |
2022-03-28 23:32:38 | vstinner | set | recipients:
+ vstinner, rhettinger, pitrou, eric.smith, petr.viktorin, serhiy.storchaka, JelleZijlstra, corona10 |
2022-03-28 23:32:38 | vstinner | set | messageid: <1648510358.75.0.470938350738.issue47143@roundup.psfhosted.org> |
2022-03-28 23:32:38 | vstinner | link | issue47143 messages |
2022-03-28 23:32:38 | vstinner | create | |
|