Python's equivalent to null-conditional operator introduced in C# 6

后端 未结 4 2030
时光说笑
时光说笑 2021-01-01 16:04

Is there an equivalent in Python to C# null-conditional operator?

System.Text.StringBuilder sb = null;
string s = sb?.ToString(); // No error
4条回答
  •  臣服心动
    2021-01-01 17:03

    There's a proposal under PEP-505, in the meantime there's a library for this:

    from pymaybe import maybe
    
    print(maybe(None).toString())
    

提交回复
热议问题