装饰类成员函数 [英] Decorating class member functions
问题描述
有没有人知道如何装饰类成员函数?
以下代码给出了一个错误:
Traceback(最新版本)最后拨打):
文件decorators2.py,第33行,在< module>
s.update()
文件" decorators2.py",第13行,在__call__
retval = self.fn .__ call __(* args,** kws)
TypeError:update()完全取得1个参数(0给定)
------------------
#! / usr / bin / env python
class Bugger(对象):
def __init __(self,module,fn):
self.module = module
self.fn = fn
def __call__(self,* args,** kws):
ret_val = self.fn(* args,** kws)
返回ret_val
def instrument(module_name):
ret_val = lambda x:Bugger(module_name,x)
返回ret_val
类愚蠢:
def __init __(个体经营) :
self.val = 1
@instrument(" xpd.spam")
def update(self):
self.val + = 1
s =愚蠢()
s.update()
哦我应该提一下装饰者需要有一些状态概念
(比如上面的类)
>
5月3日晚上9:21,Andy Terrel< andy.ter ... @ gmail.comwrote:
好吧有人知道吗要装饰类成员函数吗?
以下代码给出了一个错误:
回溯(最近一次调用最后一次):
文件" decorators2.py",第33行,< module>
s.update()
文件decorators2.py,第13行,在__call__
retval = self.fn .__ call __(* args,** kws)
TypeError:update()只取1个参数(0给定)
------------------
#! / usr / bin / env python
class Bugger(对象):
def __init __(self,module,fn):
self.module = module
self.fn = fn
def __call__(self,* args,** kws):
ret_val = self.fn(* args,** kws)
返回ret_val
def instrument(module_name):
ret_val = lambda x:Bugger(module_name,x)
返回ret_val
类愚蠢:
def __init __(个体经营) :
self.val = 1
@instrument(" xpd.spam")
def update(self):
self.val + = 1
s =愚蠢()
s.update()
装饰器是一个带有一个参数的函数:一个函数。
" instrument"必须归还装饰者。
5月3日晚9点33分,Virgil Dupras< hardcoded.softw ... @ gmail.comwrote:
5月3日晚9点21分,Andy Terrel< andy.ter ... @ gmail.comwrote:
好的,有谁知道如何装饰类成员函数?
以下代码给出了一个错误:
Traceback(最近的电话最后一次):
文件" decorators2.py",第33行,在< module>
s.update()
文件decorators2.py,第13行,在__call__
retval = self.fn .__ call __(* args,** kws)
TypeError:update()需要1个参数(0给定)
------------------
#! / usr / bin / env python
class Bugger(object):
def __init __(self,module,fn) :
self.module = module
self.fn = fn
def __call__( self,* args,** kws):
ret_val = self.fn(* args,** kws)
return ret_val
< blockquote class =post_quotes>
def instrument(module_name):
ret_val = lambda x:Bugger(module_name,x)
return ret_val
class Stupid:
def __init __(self):
self.val = 1
@instrument(" xpd.spam")
def update(self):
self。 val + = 1
s = Stupid()
s.update()
装饰器是一个带有一个参数的函数:一个函数。
" instrument"必须返回装饰者。
等等,我只是为自己感到尴尬。没关系我的上一篇文章。
Okay does anyone know how to decorate class member functions?
The following code gives me an error:
Traceback (most recent call last):
File "decorators2.py", line 33, in <module>
s.update()
File "decorators2.py", line 13, in __call__
retval = self.fn.__call__(*args,**kws)
TypeError: update() takes exactly 1 argument (0 given)
------------------
#! /usr/bin/env python
class Bugger (object):
def __init__ (self, module, fn):
self.module = module
self.fn = fn
def __call__ (self,*args, **kws):
ret_val = self.fn(*args,**kws)
return ret_val
def instrument (module_name):
ret_val = lambda x: Bugger(module_name, x)
return ret_val
class Stupid:
def __init__(self):
self.val = 1
@instrument("xpd.spam")
def update(self):
self.val += 1
s = Stupid()
s.update()
Oh I should mention the decorator needs to have some notion of state
(such as with the above class)
On May 3, 9:21 pm, Andy Terrel <andy.ter...@gmail.comwrote:Okay does anyone know how to decorate class member functions?
The following code gives me an error:
Traceback (most recent call last):
File "decorators2.py", line 33, in <module>
s.update()
File "decorators2.py", line 13, in __call__
retval = self.fn.__call__(*args,**kws)
TypeError: update() takes exactly 1 argument (0 given)
------------------
#! /usr/bin/env python
class Bugger (object):
def __init__ (self, module, fn):
self.module = module
self.fn = fn
def __call__ (self,*args, **kws):
ret_val = self.fn(*args,**kws)
return ret_val
def instrument (module_name):
ret_val = lambda x: Bugger(module_name, x)
return ret_val
class Stupid:
def __init__(self):
self.val = 1
@instrument("xpd.spam")
def update(self):
self.val += 1
s = Stupid()
s.update()A decorator is a function that takes one single parameter: a function.
"instrument" must return a decorator.
On May 3, 9:33 pm, Virgil Dupras <hardcoded.softw...@gmail.comwrote:On May 3, 9:21 pm, Andy Terrel <andy.ter...@gmail.comwrote:
Okay does anyone know how to decorate class member functions?
The following code gives me an error:
Traceback (most recent call last):
File "decorators2.py", line 33, in <module>
s.update()
File "decorators2.py", line 13, in __call__
retval = self.fn.__call__(*args,**kws)
TypeError: update() takes exactly 1 argument (0 given)
------------------
#! /usr/bin/env python
class Bugger (object):
def __init__ (self, module, fn):
self.module = module
self.fn = fn
def __call__ (self,*args, **kws):
ret_val = self.fn(*args,**kws)
return ret_val
def instrument (module_name):
ret_val = lambda x: Bugger(module_name, x)
return ret_val
class Stupid:
def __init__(self):
self.val = 1
@instrument("xpd.spam")
def update(self):
self.val += 1
s = Stupid()
s.update()
A decorator is a function that takes one single parameter: a function.
"instrument" must return a decorator.Oh wait, I just embarrassed myself. Nevermind my last post.
这篇关于装饰类成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!