None 在 Python 链式属性访问中传播

None propagation in Python chained attribute access

在Python中是否有空传播运算符("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

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

特别是 PEP 505 中提出的运算符之一是

The "None-aware attribute access" operator ?. ("maybe dot") evaluates the complete expression if the left hand side evaluates to a value that is not None