我想创建一个代理类,它包装int
以进行线程安全访问。与内置类型相比,代理类是可变的,因此它可以就地递增。现在,我想将该类用作外部的普通整数。通常,Python的__getattr__
可以很容易地将属性访问转发到内部对象:
class Proxy:
def __init__(self, initial=0):
self._lock = threading.Lock()
self._value = initial
def increment(self):
with self._lock:
self._value += 1
def __getattr__(self, name):
return getattr(self._value, name)
然而,__getattr__
does not get triggered for magic methods类似于__add__
,__rtruediv__
等我需要让代理行为像整数一样。有没有办法自动生成这些方法,或者将它们转发到包装的整数对象?
答案 0 :(得分:2)
@VPfB中由comments链接的博客文章对内置类型的代理dunder方法有一个更通用和彻底的解决方案,但这是一个简化而相当野蛮的例子。我希望它有助于理解如何创建这样的转发方法。
import threading
import numbers
def _proxy_slotted(name):
def _proxy_method(self, *args, **kwgs):
return getattr(self._value, name)(*args, **kwgs)
# Not a proper qualname, but oh well
_proxy_method.__name__ = _proxy_method.__qualname__ = name
return _proxy_method
# The list of abstract methods of numbers.Integral
_integral_methods = """
__abs__ __add__ __and__ __ceil__ __eq__ __floor__
__floordiv__ __int__ __invert__ __le__ __lshift__
__lt__ __mod__ __mul__ __neg__ __or__ __pos__ __pow__
__radd__ __rand__ __rfloordiv__ __rlshift__ __rmod__
__rmul__ __ror__ __round__ __rpow__ __rrshift__
__rshift__ __rtruediv__ __rxor__ __truediv__ __trunc__
__xor__""".split()
# The dunder, aka magic methods
_Magic = type('_Magic', (),
{name: _proxy_slotted(name)
for name in _integral_methods})
class IntProxy(_Magic, numbers.Integral):
"""
>>> res = IntProxy(1) + 1
>>> print(type(res), res)
<class 'int'> 2
>>> print(IntProxy(2) / 3)
0.6666666666666666
"""
def __init__(self, initial=0, Lock=threading.Lock):
self._lock = Lock()
self._value = initial
def increment(self):
with self._lock:
self._value += 1
def __getattr__(self, name):
return getattr(self._value, name)