如何在不将其更改为非静态的情况下更改方法实现
How to change method implementation without changing it to be not static
例如:
class a:
@staticmethod
def aaa():
print 'a'
a.aaa()
a.aaa = lambda: raise ValueError('a')
a.aaa()
第二次 python 引发错误,我没有将 class a 的实例传递到方法中。
如何在不删除静态 属性?
的情况下更改实现
直接在上面调用staticmethod
装饰器:
class a:
@staticmethod
def aaa():
print 'a'
a.aaa()
# decorate the lambda expression with staticmethod
a.aaa = <b>staticmethod(</b>lambda: 'a'<b>)</b>
a.aaa()
这基本上就是您在使用装饰器时所做的事情。
注意你不能raise
直接报错,因为raise
是一个语句,不是表达式。但是,您可以 use some tricks to raise an exception in a lambda expression.
例如:
class a:
@staticmethod
def aaa():
print 'a'
a.aaa()
a.aaa = lambda: raise ValueError('a')
a.aaa()
第二次 python 引发错误,我没有将 class a 的实例传递到方法中。 如何在不删除静态 属性?
的情况下更改实现直接在上面调用staticmethod
装饰器:
class a:
@staticmethod
def aaa():
print 'a'
a.aaa()
# decorate the lambda expression with staticmethod
a.aaa = <b>staticmethod(</b>lambda: 'a'<b>)</b>
a.aaa()
这基本上就是您在使用装饰器时所做的事情。
注意你不能raise
直接报错,因为raise
是一个语句,不是表达式。但是,您可以 use some tricks to raise an exception in a lambda expression.