推荐学习书目
› 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 · 3341 views
    This topic created in 1880 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
    Bazingal
    Sep 9, 2021❤️3
    1
    建议直接抄 C#,反正 JavaScript 很多也是借鉴 C#的,比如上面提到的两个
    abersheeran
    abersheeran
    OP
    Sep 9, 2021
    2
    @Bazingal C# 没怎么看过,就拿来做过一次大学的作业😂我正经用过的语言只有 JavaScript 、Python 、C 、Rust 、Go……有空我去看看它的设计
    yazinnnn
    yazinnnn
    Sep 9, 2021
    3
    https://clojure.org/guides/threading_macros

    建议把 pipe 改成 Clojure 的串行宏
    abersheeran
    abersheeran
    OP
    Sep 9, 2021
    4
    @Bazingal 好家伙,C# 的 ?? 是右结合的,我设计的是左结合的。我赶紧去注明一下。
    abersheeran
    abersheeran
    OP
    Sep 9, 2021
    5
    @yazinnnn 😓你这个,请去 https://github.com/hylang/hy 提。
    Ediacaran
    Ediacaran
    Sep 9, 2021 via iPhone
    6
    c=a or b
    Rwing
    Rwing
    Sep 9, 2021
    7
    C# 的 ??= 和 ?. 直接抄就好
    2i2Re2PLMaDnghL
    2i2Re2PLMaDnghL
    Sep 9, 2021
    8
    @abersheeran 这个左结合和右结合有什么区别啊
    Asjun
    Asjun
    Sep 9, 2021
    9
    C# 的 ?? 也是先看左侧的吧,a ?? b 等于
    if (a != null) a
    else b
    abersheeran
    abersheeran
    OP
    Sep 9, 2021
    10
    @2i2Re2PLMaDnghL a ?? b ?? c 左结合就从左开始算起,右结合从右算起。
    abersheeran
    abersheeran
    OP
    Sep 9, 2021
    11
    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
    Asjun
    Sep 10, 2021
    12
    @abersheeran 学到了,以前一直以为 C#的是左结合😂
    2i2Re2PLMaDnghL
    2i2Re2PLMaDnghL
    Sep 10, 2021
    13
    @abersheeran 我的意思是,这个操作不是符合结合律的吗?行为上左结合和右结合没有区别啊
    如果 a 非 null,则无论左结合还是右结合都不会计算 b 或 c ;如果 b 非 null 无论左结合还是右结合都不会计算 c
    至于有区别的是 C#独有的 ??= ,左结合就根本没法运算,因为左边的 ??= 出来的不是一个左值。
    abersheeran
    abersheeran
    OP
    Sep 10, 2021
    14
    @2i2Re2PLMaDnghL 我想了一下,好像在这一个操作符上确实没什么区别。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5804 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 02:26 · PVG 10:26 · LAX 18:26 · JFK 21:26
    ♥ Do have faith in what you're doing.