推荐学习书目
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
abersheeran
V2EX  ›  Python

“鸣蛇”再添新功能:空值合并

  •  
  •   abersheeran · Sep 9, 2021 · 2902 views
    This topic created in 1722 days ago, the information mentioned may be changed or developed.

    https://github.com/abersheeran/mingshe

    写过 JavaScript 的人应该很熟悉的 ?? 运算符。a ?? b 等价于 a if a is not None else b

    预计还会增加 JavaScript 里的 ?. 操作符,但还在考量之中。欢迎大家使用、提意见。

    14 replies    2021-09-10 11:35:01 +08:00
    Bazingal
        1
    Bazingal  
       Sep 9, 2021   ❤️ 3
    建议直接抄 C#,反正 JavaScript 很多也是借鉴 C#的,比如上面提到的两个
    abersheeran
        2
    abersheeran  
    OP
       Sep 9, 2021
    @Bazingal C# 没怎么看过,就拿来做过一次大学的作业😂我正经用过的语言只有 JavaScript 、Python 、C 、Rust 、Go……有空我去看看它的设计
    yazinnnn
        3
    yazinnnn  
       Sep 9, 2021
    https://clojure.org/guides/threading_macros

    建议把 pipe 改成 Clojure 的串行宏
    abersheeran
        4
    abersheeran  
    OP
       Sep 9, 2021
    @Bazingal 好家伙,C# 的 ?? 是右结合的,我设计的是左结合的。我赶紧去注明一下。
    abersheeran
        5
    abersheeran  
    OP
       Sep 9, 2021
    @yazinnnn 😓你这个,请去 https://github.com/hylang/hy 提。
    Ediacaran
        6
    Ediacaran  
       Sep 9, 2021 via iPhone
    c=a or b
    Rwing
        7
    Rwing  
       Sep 9, 2021
    C# 的 ??= 和 ?. 直接抄就好
    2i2Re2PLMaDnghL
        8
    2i2Re2PLMaDnghL  
       Sep 9, 2021
    @abersheeran 这个左结合和右结合有什么区别啊
    Asjun
        9
    Asjun  
       Sep 9, 2021
    C# 的 ?? 也是先看左侧的吧,a ?? b 等于
    if (a != null) a
    else b
    abersheeran
        10
    abersheeran  
    OP
       Sep 9, 2021
    @2i2Re2PLMaDnghL a ?? b ?? c 左结合就从左开始算起,右结合从右算起。
    abersheeran
        11
    abersheeran  
    OP
       Sep 9, 2021
    https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/null-coalescing-operator

    C# 的 a ?? b ?? c 等价于 a ?? (b ?? c)
    mingshe 的 a ?? b ?? c 等价于 (a ?? b) ?? c
    Asjun
        12
    Asjun  
       Sep 10, 2021
    @abersheeran 学到了,以前一直以为 C#的是左结合😂
    2i2Re2PLMaDnghL
        13
    2i2Re2PLMaDnghL  
       Sep 10, 2021
    @abersheeran 我的意思是,这个操作不是符合结合律的吗?行为上左结合和右结合没有区别啊
    如果 a 非 null,则无论左结合还是右结合都不会计算 b 或 c ;如果 b 非 null 无论左结合还是右结合都不会计算 c
    至于有区别的是 C#独有的 ??= ,左结合就根本没法运算,因为左边的 ??= 出来的不是一个左值。
    abersheeran
        14
    abersheeran  
    OP
       Sep 10, 2021
    @2i2Re2PLMaDnghL 我想了一下,好像在这一个操作符上确实没什么区别。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4559 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 76ms · UTC 09:59 · PVG 17:59 · LAX 02:59 · JFK 05:59
    ♥ Do have faith in what you're doing.