我在下面有一些代码
class Example:
def __init__(self,height,weight)
self.height = height
self.weight = weight
@staticmethod
def some_op(func)
def inner(*args,**kwargs)
s = func(*args,**kwargs)
print("Implementing function...")
@some_op
def num_op(self,values):
for value in values:
v = value * 10
q = v - 100
c = q ** -1
return c
example = Example()
values = [11,23123,1209,234]
example.num_op(values)
但它输出:
TypeError: 'staticmethod' object is not callable
我真的不知道类中的装饰器,所以我应该如何更改代码以使其返回:
Implementing function...
0.0004464285714285714
非常感谢!
静态方法不可调用;它是一个对象,其__get__
方法返回一个可调用对象。但是,您不是some_op
作为属性访问(不完整的定义除外),而是作为常规函数访问,因此它的__get__
方法永远不会被使用。您有两个选择:
some_op
为类外的常规函数。some_op
为静态方法。由于您只是在类定义本身内部调用它,因此让它成为常规函数,并且不要将其用作实例方法。(您可以将其定义为_some_op
强调不应在课堂外使用。)有关__get__
它是什么以及它如何工作的更多信息,请参阅Descriptor HowTo Guide和特别是关于静态方法的部分。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句