sa = SomeActor() sa.meth.add_callback(回调) #现在这个 sa.meth(我是一个无聊的旧论点) #我想产生这个 >>我是一个无聊的旧论点 Yippie,我已被召唤< __ main__。 Somebctor对象位于0xb7e8b40c> #左右 这比预期更复杂,主要有两个原因: *我无法找到一种方法将正确的实例参数传递给 回调,也就是说,我不是知道如何检索 meth()被调用的实例,因为可观察的装饰器只获得* b $ b * function *对象而不是*方法*。 (我希望这很清楚 够了) *另外,我也没看到如何将add_callback()方法添加到 meth对象。这似乎不可能。我可以将它添加到meth'的 函数对象中,只是在observable的定义中可以正常使用,但是我这不是我真正想要的。这可能只是一个化妆品问题因为我不喜欢打电话给 sa.meth.im_func.add_callback(回调)。 有什么想法吗?会很棒。 / W 解决方案 Wildemar Wildenburger写道: 我想:我会写一个装饰器让我对方法做出反应 轻松调用(这是一个非常流行的观察者模式)。我看了一些 接收,但我只是没有得到它们(我今天感觉有点愚蠢,对不起)。 [snip] 这比预期更复杂,主要有两个原因: *我无法找到一种方法将正确的实例参数传递给 回调,也就是说,我不知道如何检索调用 meth()的实例,因为可观察装饰器只获得* * * function *对象而不是*方法*。 (我希望这很清楚 够了) *另外,我也没看到如何将add_callback()方法添加到 meth对象。这似乎不可能。我可以将它添加到meth'的 函数对象中,只是在observable的定义中可以正常使用,但是我这不是我真正想要的。这可能只是一个化妆品问题因为我不喜欢打电话给 sa.meth.im_func.add_callback(回调)。 我想你想在你的Observable类上定义__get__,这样当它绑定到方法时,它可以做正确的事情实例: >> class Observable(object): .... def __init __(self,func,instance = None,observers = None): ....如果观察者是无: ....观察者= [] .... self.func = func .... self.instance = instance .... self.observers =观察员 .... def __get __(self,obj,cls = None): ....如果obj是None: ....返回自我 ....其他: 。 ... func = self.func .__ get __(obj,cls) .... return Observable(func,obj,self.observers) .... def __ca ll __(self,* args,** kwargs): .... result = self.func(* args,** kwargs) .... for self.observers中的观察者: ....观察者(self.instance) ....返回结果 .... def add_callback(self,callback): .... self.observers.append(callback) .... >> class SomeActor(object): .... @Observable .... def meth(self,foo): .... print foo .... >> def callback(instance): .... print" Yippie,我已经被召唤了,实例 .... instance.bar = True .... >> sa = SomeActor()sa.meth.add_callback(callback) sa.meth(我是一个无聊的旧论点) 我是一个无聊的旧论点 Yippie,我已被调用< __ main __。SomeActor对象位于0x00E7A4D0> >> sa.bar True STeVe Steven Bethard写道: 我想你想在你的Observable类上定义__get__,以便当方法绑定到实例时它可以做正确的事情: >> class Observable(object): [snip] ] ... def __get __(self,obj,cls = None): ...如果obj是None: ..返回自我 ...其他: ... func = self.func .__ get __(obj,cls) ...返回Observable(func,obj,self.observers) [snip] 很棒,就是这样!谢谢你十亿:) 我花了很多时间来理解__get__方法的作用, 但是我觉得现在我想出来了。我已经用我的理解Python(特别是方法绑定)了解了这个!真棒! :) / W En Fri,01 Jun 2007 21:25:50 -0300,Wildemar Wildenburger < wi ****** @freakmail.deescribió: Steven Bethard写道: >我想你想在Observable类上定义__get__,以便它当方法绑定到实例时,可以做正确的事: 我花了很多时间了解__get__方法的作用, 但我想现在我明白了。我已经用我的理解Python(特别是方法绑定)了解了这个!真棒! 要进入下一个能量水平,请阅读中的一些文章 http://www.python.org/doc/newstyle/ - Gabriel Genellina Hello folks :)This has got to be an FAQ, but somehow I can''t seem to find an answerthat I understand ... I thought: I''ll just write a decorator that lets me react to methodcalls easily (the ever so popular observer-pattern). I''ve looked at somerecepies, but I just don''t get them (I''m feeling kinda dumb today, sorry).What I''d *like* to do is this: def observable(func):# magic code class SomeActor(object):@observabledef meth(self, foo):print foo def callback(instance):print "Yippie, I''ve been called on", instanceinstance.bar = True sa = SomeActor()sa.meth.add_callback(callback) # now thissa.meth("I''m the boring old argument") # I would like to result in this >>I''m the boring old argumentYippie, I''ve been called on <__main__.SomeActor object at 0xb7e8b40c> # or so This is more complicated than expected mainly for two reasons: * I can''t find a way to pass the proper ''instance'' argument tocallback, that is, I don''t know how to retrieve the instance thatmeth() was called on, because the observable decorator only getsthe *function* object but not the *method*. (I hope this was clearenough)* Also, I don''t see how I could add the add_callback() method to themeth object. That doesn''t seem possible. I can add it to meth''sfunction object just fine in the definition of observable, but Ithats not what I really want. This is probably just a cosmeticissue because I don''t like the idea of callingsa.meth.im_func.add_callback(callback).Any ideas? Would be great./W 解决方案 Wildemar Wildenburger wrote:I thought: I''ll just write a decorator that lets me react to methodcalls easily (the ever so popular observer-pattern). I''ve looked at somerecepies, but I just don''t get them (I''m feeling kinda dumb today, sorry).[snip]This is more complicated than expected mainly for two reasons: * I can''t find a way to pass the proper ''instance'' argument to callback, that is, I don''t know how to retrieve the instance that meth() was called on, because the observable decorator only gets the *function* object but not the *method*. (I hope this was clear enough) * Also, I don''t see how I could add the add_callback() method to the meth object. That doesn''t seem possible. I can add it to meth''s function object just fine in the definition of observable, but I thats not what I really want. This is probably just a cosmetic issue because I don''t like the idea of calling sa.meth.im_func.add_callback(callback). I think you want to define __get__ on your Observable class so that itcan do the right thing when the method is bound to the instance: >>class Observable(object): .... def __init__(self, func, instance=None, observers=None):.... if observers is None:.... observers = [].... self.func = func.... self.instance = instance.... self.observers = observers.... def __get__(self, obj, cls=None):.... if obj is None:.... return self.... else:.... func = self.func.__get__(obj, cls).... return Observable(func, obj, self.observers).... def __call__(self, *args, **kwargs):.... result = self.func(*args, **kwargs).... for observer in self.observers:.... observer(self.instance).... return result.... def add_callback(self, callback):.... self.observers.append(callback).... >>class SomeActor(object): .... @Observable.... def meth(self, foo):.... print foo.... >>def callback(instance): .... print "Yippie, I''ve been called on", instance.... instance.bar = True.... >>sa = SomeActor()sa.meth.add_callback(callback)sa.meth("I''m the boring old argument") I''m the boring old argumentYippie, I''ve been called on <__main__.SomeActor object at 0x00E7A4D0> >>sa.bar TrueSTeVeSteven Bethard wrote:I think you want to define __get__ on your Observable class so that itcan do the right thing when the method is bound to the instance: >>class Observable(object): [snip]... def __get__(self, obj, cls=None):... if obj is None:... return self... else:... func = self.func.__get__(obj, cls)... return Observable(func, obj, self.observers)[snip]Great, that does it! Thanks a billion :) It took me quite some time understanding what the __get__ method does,but I think now I figured it out. I''ve made a quantum leap in myunderstanding of Python (esp. method binding) with this! Awesome! :)/W En Fri, 01 Jun 2007 21:25:50 -0300, Wildemar Wildenburger<wi******@freakmail.deescribió: Steven Bethard wrote: >I think you want to define __get__ on your Observable class so that itcan do the right thing when the method is bound to the instance: It took me quite some time understanding what the __get__ method does,but I think now I figured it out. I''ve made a quantum leap in myunderstanding of Python (esp. method binding) with this! Awesome!To proceed to next energy level, read some articles from http://www.python.org/doc/newstyle/ --Gabriel Genellina 这篇关于观察者 - 模式由(简单)装饰者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 19:01