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
iptux
V2EX  ›  Python

今天写了一个三重 def 的 Python 函数

  •  
  •   iptux · 2015-07-27 21:14:32 +08:00 · 3652 次点击
    这是一个创建于 3188 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # decorator wrapper for _Parser()
    def Parser(cls, cache = False):
        def _fun(fun):
            def _Fun(*arg, **kw):
                # try get from cache
                if cache and (len(arg) >= 1 or '_id' in kw):
                    _id = arg[0] if arg else kw['_id']
                    o = Cacher(cls).get(_id)
                    if o:
                        return o
                ret = fun(*arg, **kw)
                try:
                    ret = _Parser(ret, cls)
                except Exception as e:
                    logging.exception('failed to parse')
                return ret
            return _Fun
        return _fun
    

    主要是为了对函数的返回值做解析并缓存

    大概是这样用的

    @Parser(Type, True)
    def get_type(_id):
        ...
    

    现在脑袋有点晕

    13 条回复    2015-07-28 12:50:36 +08:00
    ligyxy
        1
    ligyxy  
       2015-07-27 21:26:09 +08:00 via Android
    看看PEP上的例子就不晕了
    gtv
        2
    gtv  
       2015-07-27 21:32:01 +08:00
    len(arg) >= 1 可以优化为 if len?
    gtv
        3
    gtv  
       2015-07-27 21:32:37 +08:00
    说错了 if arg
    gamexg
        4
    gamexg  
       2015-07-27 22:12:11 +08:00 via Android
    昨天也写了一个,也是缓存,不过我是在第二个函数内生成的cahce。
    Python3 标准库自带了一个
    julyclyde
        5
    julyclyde  
       2015-07-27 22:39:50 +08:00
    没看懂调用方法……
    Parser返回_fun,用_fun(cls)代替原cls?
    hitsmaxft
        6
    hitsmaxft  
       2015-07-27 23:08:11 +08:00 via iPhone
    足以见得python不支持匿名函数这个语法糖,非要偷懒用局部函数定义,是多么大的损失。
    DeanThompson
        7
    DeanThompson  
       2015-07-27 23:11:47 +08:00
    julyclyde
        8
    julyclyde  
       2015-07-27 23:13:13 +08:00
    @DeanThompson 我知道是decorator,但没见过这么厚的……想象力不够用了
    bcxx
        9
    bcxx  
       2015-07-27 23:13:50 +08:00
    这种写法应该用 class 来代替的…… 重载 __call__ 方法就好了……

    另外建议看看 pep8 ~,~
    fatpa
        10
    fatpa  
       2015-07-28 01:34:58 +08:00
    不就是一装饰器么……………………
    ffffwh
        11
    ffffwh  
       2015-07-28 09:33:31 +08:00
    快去lambda神教洗洗脑
    ryd994
        12
    ryd994  
       2015-07-28 10:18:05 +08:00 via Android
    外面两层可以lambda
    yueyoum
        13
    yueyoum  
       2015-07-28 12:50:36 +08:00
    1, PEP8
    2, 多学学基础知识
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2775 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:37 · PVG 20:37 · LAX 05:37 · JFK 08:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.