相关疑难解决方法(0)

是否有C#null-coalescing运算符的Python等价物?

在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 null-coalescing-operator

258
推荐指数
6
解决办法
7万
查看次数

Python 链式属性访问中的无传播

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

7
推荐指数
1
解决办法
2116
查看次数