0

Is there a ?? operator in python like there is in C#? Which mean if the left side is Null/None then use the right side instead:

myVal = myVal ?? 1

will return 1 if myVal is Null. This is covenant for initialization, if this operator does not exist in python, what's the best way to mimic it?

Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174
  • It's the `or` operator. `myVal = myVal or 1` will do exactly what you want. – Samwise Jul 02 '21 at 15:23
  • `or` will work if rejecting any Falsey value is sufficient. If you specifically want to reject only `None`, you need a conditional expression; perhaps with `:=` to reduce duplication. – Carcigenicate Jul 02 '21 at 15:25

0 Answers0