【问题标题】:None propagation in Python chained attribute access [duplicate]Python链式属性访问中的无传播[重复]
【发布时间】:2020-04-23 06:46:48
【问题描述】:

在 Python 中是否有 null 传播运算符(“null-aware member access”运算符),所以我可以编写类似的东西

var = object?.children?.grandchildren?.property

在 C#、VB.NET 和 TypeScript 中,而不是

var = None if not myobject\
              or not myobject.children\
              or not myobject.children.grandchildren\
    else myobject.children.grandchildren.property

【问题讨论】:

  • @smci,OP 专门询问链式属性访问。很可能有一个副本,但我认为这不是你提出的那个。
  • @Chris:好的,请相应地重新命名、重新标记和重新措辞。 (原标题只是“Python中的null/None传播”
  • @Chris:不阅读正文就无法理解问题标题 b)它没有可以通过搜索找到的标签。这可能会导致将来出现重复。
  • @smci,我们在这里陷入困境。您的编辑已经改善了这个问题,而我的编辑则有所改善。让我们不要争论它最初是什么。我看看能不能找到更多的标签。

标签: python method-chaining null-coalescing-operator null-coalescing


【解决方案1】:

没有。有一个PEP proposing the addition of such operators,但它尚未(尚未)被接受。

具体来说,PEP 505 中提出的运算符之一是

如果左侧的计算结果不是None,则“None-aware 属性访问”运算符?.(“可能是点”)计算完整的表达式

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2017-10-13
    • 2013-03-20
    • 2011-11-05
    • 2014-04-18
    相关资源
    最近更新 更多