V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · May 21, 2020 · 2564 views
    This topic created in 2168 days ago, the information mentioned may be changed or developed.

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

    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 replies    2020-05-21 15:53:47 +08:00
    Ritter
        1
    Ritter  
       May 21, 2020
    ???
    ungrown
        2
    ungrown  
    OP
       May 21, 2020
    @Ritter #1 想不通能拿来干嘛是吧?
    no1xsyzy
        3
    no1xsyzy  
       May 21, 2020   ❤️ 1
    vd = VoidDuck()
    vd.__class__.__getattr__ = lambda self, key: None
    assert VoidDuck().item is None
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1008 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 19:16 · PVG 03:16 · LAX 12:16 · JFK 15:16
    ♥ Do have faith in what you're doing.