Is there a Python equivalent of the C# null-coalescing operator?

In C# there’s a null-coalescing operator (written as ??) that allows for easy (short) null checking during assignment:

string s = null;
var other = s ?? "some default value";

Is there a python equivalent?

I know that I can do:

s = None
other = s if s else "some default value"

But is there an even shorter way (where I don’t need to repeat s)?

11 Answers
11

Leave a Comment