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

[捣蛋 py] 虚空之鸭 VoidDuck

  •  
  •   ungrown ·
    ungrown · 2020-05-21 10:55:36 +08:00 · 2132 次点击
    这是一个创建于 1407 天前的主题,其中的信息可能已经有所发展或是发生改变。

    虚空之鸭,万能的鸭鸭,无能的鸭鸭,随你怎么对待它,鸭鸭不反抗,鸭鸭不回答

    https://github.com/mo-han/mo-han-toolbox/blob/master/lib_misc.py#L29

    class VoidDuck:
        """a void, versatile, useless and quiet duck, called in any way, return nothing, raise nothing"""
        def __init__(self, *args, **kwargs):
            pass
    
        def __getattr__(self, item):
            return self
    
        def __call__(self, *args, **kwargs):
            return self
    
        def __bool__(self):
            return False
    
    3 条回复    2020-05-21 15:53:47 +08:00
    Ritter
        1
    Ritter  
       2020-05-21 14:09:23 +08:00
    ???
    ungrown
        2
    ungrown  
    OP
       2020-05-21 14:44:49 +08:00
    @Ritter #1 想不通能拿来干嘛是吧?
    no1xsyzy
        3
    no1xsyzy  
       2020-05-21 15:53:47 +08:00   ❤️ 1
    vd = VoidDuck()
    vd.__class__.__getattr__ = lambda self, key: None
    assert VoidDuck().item is None
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1003 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 19:27 · PVG 03:27 · LAX 12:27 · JFK 15:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.