在C#中有一个null-coalescing运算符(写为??),允许在赋值期间进行简单(短)空检查:
string s = null;
var other = s ?? "some default value";
Run Code Online (Sandbox Code Playgroud)
是否有python等价物?
我知道我能做到:
s = None
other = s if s else "some default value"
Run Code Online (Sandbox Code Playgroud)
但是有更短的方式(我不需要重复s)?
Python中是否有一个空传播运算符(“空感知成员访问”运算符),所以我可以写一些类似的东西
var = object?.children?.grandchildren?.property
Run Code Online (Sandbox Code Playgroud)
就像在 C#、VB.NET 和 TypeScript 中一样,而不是
var = None if not myobject\
or not myobject.children\
or not myobject.children.grandchildren\
else myobject.children.grandchildren.property
Run Code Online (Sandbox Code Playgroud) python null-coalescing-operator null-coalescing method-chaining