我在Python中装饰静态方法时遇到了一些问题.我认为以下代码最能代表我的问题:
def decorator(func):
print callable(func)
return func
class Foo():
@decorator
@staticmethod
def bar():
return
# outputs False
print callable(Foo.bar)
# outputs True
Run Code Online (Sandbox Code Playgroud)
这似乎是一个错误.我想它会产生,因为当方法Foo.bar
传递给装饰器时,它是一个函数,而不是一个方法.这是我可以看到它不可调用的唯一原因,因为如果我们装饰一个标准函数,它就不可调用,如下所示.
@staticmethod
def function():
return
print callable(function)
# outputs False
Run Code Online (Sandbox Code Playgroud)
那么这是实现staticmethod装饰器的真正错误,和/或有任何简单的解决方法吗?我确实想过写一个装饰器来设置__call__
属性,但我不知道如何callable
实现,所以我无法衡量这种方法的成功.
归档时间: |
|
查看次数: |
1869 次 |
最近记录: |