python method decorate_Python decorator作为staticmethod

我正在尝试编写一个python类,该类使用需要实例状态信息的decorator函数。这是按预期工作的,但是如果我显式地将decorator设为staticmetod,则会得到以下错误:Traceback (most recent call last):

File "tford.py", line 1, in

class TFord(object):

File "tford.py", line 14, in TFord

@ensure_black

TypeError: 'staticmethod' object is not callable

为什么?

代码如下:class TFord(object):

def __init__(self, color):

self.color = color

@staticmethod

def ensure_black(func):

def _aux(self, *args, **kwargs):

if self.color == 'black':

return func(*args, **kwargs)

else:

return None

return _aux

@ensure_black

def get():

return 'Here is your shiny new T-Ford'

if __name__ == '__main__':

ford_red = TFord('red')

ford_black = TFord('black')

print ford_red.get()

print ford_black.get()

如果我只删除@staticmethod行,一切都正常,但我不明白为什么。它不需要self作为第一个参数吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值