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

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    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)?

    Continue reading...

Compartilhe esta Página