Python 的装饰器能不能写到类里面 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
334862132
V2EX    Python

Python 的装饰器能不能写到类里面

  •  
  •   334862132 2019 年 4 月 1 日 3904 次点击
    这是一个创建于 2476 天前的主题,其中的信息可能已经有所发展或是发生改变。
    主要一个装饰器只校验一个接口,扔类外面感觉太乱,试了几种方法,都不好使,直接上代码,这个是一个报错的写法静态方法不支持的说... TypeError: 'staticmethod' object is not callable
    还有类方法也用过,不过好像也报错,直接用 self.还是报错,也不知道是不是我写错了
    import datetime
    class test():
    # 时间校验
    @staticmethod
    def timeDecorator(fun):
    timestart = datetime.datetime(today().year, today().month, today().day, 0, 0, 0)
    orderend = datetime.datetime(func.today().year, func.today().month, func.today().day, 1, 0, 0)
    timeend = datetime.datetime(today().year, today().month, today().day, 23, 59, 59)
    orderstart = datetime.datetime(func.today().year, func.today().month, func.today().day, 23, 0, 0)
    now = datetime.datetime.now()

    def timeVerify(*args, **kwargs):
    if (now > orderstart and now < timeend) or (now > timestart and now < orderend):
    return print('系统维护中,23:00~01:00 无法使用,请于其他时间操作')
    else:
    return fun(*args, **kwargs)

    return timeVerify

    @timeDecorator
    def prin(self):
    print('pass')
    return

    test().prin()
    9 条回复    2019-04-01 14:00:23 +08:00
    zzzmj
        1
    zzzmj  
       2019 年 4 月 1 日   1
    我怎么感觉楼主需要 先看看装饰器的实现
    yushenglin
        2
    yushenglin  
       2019 年 4 月 1 日
    装饰器都写错了,怎么可能能用
    tony601818
      &bsp; 3
    tony601818  
       2019 年 4 月 1 日
    这不是能不能写到类里的问题,这是根本就写的不对啊。
    334862132
        4
    334862132  
    OP
       2019 年 4 月 1 日
    @tony601818
    @yushenglin 本来写的就不对,我不过是想问问咋写 原来是这么写的 我不过是感觉这一个装饰器只装饰一个接口想把它封装到类里面 但是怎么封装都报错 ,所以贴了一个错的想问一下...
    import datetime
    def timeDecorator(fun):
    timestart = func.timestart()
    orderend = datetime.datetime(func.today().year, func.today().month, func.today().day, 1, 0, 0)
    timeend = func.timeend()
    orderstart = datetime.datetime(func.today().year, func.today().month, func.today().day, 23, 0, 0)
    now = datetime.datetime.now()

    def timeVerify(*args, **kwargs):
    if (now > orderstart and now < timeend) or (now > timestart and now < orderend):
    return print('系统维护中,23:00~01:00 无法使用,请于其他时间操作')
    else:
    return fun(*args, **kwargs)

    return timeVerify

    class test():
    # 时间校验
    @timeDecorator
    def prin(self):
    print('pass')
    return

    test().prin()
    STRRL
        5
    STRRL  
       2019 年 4 月 1 日   1
    装饰器的前提是 OOP
    no1xsyzy
        6
    no1xsyzy  
       2019 年 4 月 1 日   1
    @334862132 你还是贴 gist 吧,眼 parse 掉空格的 python 要卿命了
    fy
        7
    fy  
       2019 年 4 月 1 日   1
    单论装饰器在类上的替代实现,可以使用 meta class
    no1xsyzy
        8
    no1xsyzy  
       2019 年 4 月 1 日   1
    另外,“想把它封装到类里面” 是个什么意思?请把你期望的行为贴出来。

    按我猜的行为,你 #4 写的也是错的。
    如果你是说所有函数在调用的时候都被 around 的话可以考虑 MetaClass。
    xiaket
        9
    xiaket  
       2019 年 4月 1 日   1
    我也觉得不应该把这个装饰器放到这个类里面, 可以放到含有这个类的文件里面. 当然除非你这个文件里面已经有很多很多的类了, 那么我个人会写一个 utils.py 文件, 来放这些工具类的东西.

    关键是, 我看不到这个装饰器里包含任何类属性 /方法的地方. 所以逻辑上这个装饰器不属于这个类.

    当然, 如果你实在想要这样做, 可以参考这段代码:

    https://gist.github.com/xiaket/3853a44dc02b127b5f8cac762f889b1a
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2659 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 11:31 PVG 19:31 LAX 03:31 JFK 06:31
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86