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-04-01 10:05:32 +08:00 · 3148 次点击
    这是一个创建于 1823 天前的主题,其中的信息可能已经有所发展或是发生改变。
    主要一个装饰器只校验一个接口,扔类外面感觉太乱,试了几种方法,都不好使,直接上代码,这个是一个报错的写法静态方法不支持的说... 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-04-01 10:13:30 +08:00   ❤️ 1
    我怎么感觉楼主需要 先看看装饰器的实现
    yushenglin
        2
    yushenglin  
       2019-04-01 10:28:21 +08:00
    装饰器都写错了,怎么可能能用
    tony601818
        3
    tony601818  
       2019-04-01 10:30:15 +08:00
    这不是能不能写到类里的问题,这是根本就写的不对啊。
    334862132
        4
    334862132  
    OP
       2019-04-01 11:51:24 +08:00
    @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-04-01 13:42:28 +08:00   ❤️ 1
    装饰器的前提是 OOP
    no1xsyzy
        6
    no1xsyzy  
       2019-04-01 13:43:28 +08:00   ❤️ 1
    @334862132 你还是贴 gist 吧,眼 parse 掉空格的 python 要卿命了
    fy
        7
    fy  
       2019-04-01 13:45:37 +08:00   ❤️ 1
    单论装饰器在类上的替代实现,可以使用 meta class
    no1xsyzy
        8
    no1xsyzy  
       2019-04-01 13:49:35 +08:00   ❤️ 1
    另外,“想把它封装到类里面” 是个什么意思?请把你期望的行为贴出来。

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

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

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

    https://gist.github.com/xiaket/3853a44dc02b127b5f8cac762f889b1a
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5305 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 09:20 · PVG 17:20 · LAX 02:20 · JFK 05:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.