[英]Python 3 bound methods subscription
一开始,我知道绑定方法属性在Python 3中不存在(根据本主题: setattr为什么在绑定方法上失败 )
我正在尝试编写一个伪“反应性” Python框架。 也许我缺少了一些东西,也许,我想做的是可行的。 让我们看一下代码:
from collections import defaultdict
class Event:
def __init__(self):
self.funcs = []
def bind(self, func):
self.funcs.append(func)
def __call__(self, *args, **kwargs):
for func in self.funcs:
func(*args, **kwargs)
def bindable(func):
events = defaultdict(Event)
def wrapper(self, *args, **kwargs):
func(self, *args, **kwargs)
# I'm doing it this way, because we need event PER class instance
events[self]()
def bind(func):
# Is it possible to somehow implement this method "in proper way"?
# to capture "self" somehow - it has to be implemented in other way than now,
# because now it is simple function not connected to an instance.
print ('TODO')
wrapper.bind = bind
return wrapper
class X:
# this method should be bindable - you should be able to attach callback to it
@bindable
def test(self):
print('test')
# sample usage:
def f():
print('calling f')
a = X()
b = X()
# binding callback
a.test.bind(f)
a.test() # should call f
b.test() # should NOT call f
当然,对于该示例,所有类(例如Event
都得到了简化。 有什么办法可以解决此代码问题? 我只是希望能够使用bindable
装饰器来使方法(而不是函数!)可绑定,并且以后能够将其“绑定”到回调中-这样,如果有人调用该方法,则将调用该回调自动。
Python 3有什么办法做到这一点?
老实说,我没有您的问题的答案,只是另一个问题返回:
猴子修补实例不会创建您想要的行为:
#! /usr/bin/python3.2
import types
class X:
def __init__ (self, name): self.name = name
def test (self): print (self.name, 'test')
def f (self): print (self.name, '!!!')
a = X ('A')
b = X ('B')
b.test = types.MethodType (f, b) #"binding"
a.test ()
b.test ()
哦耶! :D我找到了答案-有点疯狂,但是工作很快。 如果有人有任何意见或更好的解决方案,我将很感兴趣。 以下代码适用于方法和功能:
# ----- test classes -----
class Event:
def __init__(self):
self.funcs = []
def bind(self, func):
self.funcs.append(func)
def __call__(self, *args, **kwargs):
message = type('EventMessage', (), kwargs)
for func in self.funcs:
func(message)
# ----- implementation -----
class BindFunction:
def __init__(self, func):
self.func = func
self.event = Event()
def __call__(self, *args, **kwargs):
out = self.func(*args, **kwargs)
self.event(source=None)
return out
def bind(self, func):
self.event.bind(func)
class BindMethod(BindFunction):
def __init__(self, instance, func):
super().__init__(func)
self.instance = instance
def __call__(self, *args, **kwargs):
out = self.func(self.instance, *args, **kwargs)
self.event(source=self.instance)
return out
class Descriptor(BindFunction):
methods = {}
def __get__(self, instance, owner):
if not instance in Descriptor.methods:
Descriptor.methods[instance] = BindMethod(instance, self.func)
return Descriptor.methods[instance]
def bindable(func):
return Descriptor(func)
# ----- usage -----
class list:
def __init__(self, seq=()):
self.__list = [el for el in seq]
@bindable
def append(self, p_object):
self.__list.append(p_object)
def __str__(self):
return str(self.__list)
@bindable
def x():
print('calling x')
# ----- tests -----
def f (event):
print('calling f')
print('source type: %s' % type(event.source))
def g (event):
print('calling g')
print('source type: %s' % type(event.source))
a = list()
b = list()
a.append.bind(f)
b.append.bind(g)
a.append(5)
print(a)
b.append(6)
print(b)
print('----')
x.bind(f)
x()
和输出:
calling f
source type: <class '__main__.list'>
[5]
calling g
source type: <class '__main__.list'>
[6]
----
calling x
calling f
source type: <class 'NoneType'>
诀窍是使用Python的描述符存储当前实例指针。
结果,我们能够将回调绑定到任何python函数。 执行开销不会太大- 空函数的执行速度比没有此装饰器的执行速度慢5-6倍。 这种开销是由所需的功能链和事件处理引起的。
当使用“适当的”事件实现(使用弱引用)时,如下所示: 信号插槽实现 ,我们得到的开销是基本函数执行的20到25倍,这仍然很好。
编辑:根据Hyperboreus问题,我更新了代码,以便能够从回调方法中读取调用了回调的源对象。 现在可以通过event.source
变量访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.