【发布时间】:2017-09-24 23:54:18
【问题描述】:
为了更好的封装,我想用同一个类中的方法来装饰实例方法。
class SomeClass(object):
@staticmethod
def some_decorator(func):
def wrapped(self):
print 'hello'
return func(self)
return wrapped
@some_decorator
def do(self):
print 'world'
x = SomeClass()
x.do()
但是,这段代码引发了TypeError: 'staticmethod' object is not callable
现在我通过定义一个类并重载它的 new 方法来模拟一个函数来解决这个问题,但它最终是一个类,而不是一个函数。
那么我可以在类范围内访问我的函数吗?
【问题讨论】:
标签: python