other = s or "some default value"

Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean context. If the values are not boolean, they are converted to boolean for the purposes of the operator.

Note that the or operator does not return only True or False. Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false.

In this case, the expression x or y returns x if it is True or evaluates to true when converted to boolean. Otherwise, it returns y. For most cases, this will serve for the very same purpose of C♯'s null-coalescing operator, but keep in mind:

42    or "something"    # returns 42
0     or "something"    # returns "something"
None  or "something"    # returns "something"
False or "something"    # returns "something"
""    or "something"    # returns "something"

If you use your variable s to hold something that is either a reference to the instance of a class or None (as long as your class does not define members __bool__() and __len__()), it is secure to use the same semantics as the null-coalescing operator. NB. Python 2.7 will look for __nonzero__() instead of __bool__().

In fact, it may even be useful to have this side-effect of Python. Since you know what values evaluates to false, you can use this to trigger the default value without using None specifically (an error object, for example).

In some languages this behavior is referred to as the Elvis operator.

Answer from Juliano on Stack Overflow
Top answer
1 of 16
746
other = s or "some default value"

Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean context. If the values are not boolean, they are converted to boolean for the purposes of the operator.

Note that the or operator does not return only True or False. Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false.

In this case, the expression x or y returns x if it is True or evaluates to true when converted to boolean. Otherwise, it returns y. For most cases, this will serve for the very same purpose of C♯'s null-coalescing operator, but keep in mind:

42    or "something"    # returns 42
0     or "something"    # returns "something"
None  or "something"    # returns "something"
False or "something"    # returns "something"
""    or "something"    # returns "something"

If you use your variable s to hold something that is either a reference to the instance of a class or None (as long as your class does not define members __bool__() and __len__()), it is secure to use the same semantics as the null-coalescing operator. NB. Python 2.7 will look for __nonzero__() instead of __bool__().

In fact, it may even be useful to have this side-effect of Python. Since you know what values evaluates to false, you can use this to trigger the default value without using None specifically (an error object, for example).

In some languages this behavior is referred to as the Elvis operator.

2 of 16
191

Strictly,

other = s if s is not None else "default value"

Otherwise, s = False will become "default value", which may not be what was intended.

If you want to make this shorter, try:

def notNone(s,d):
    if s is None:
        return d
    else:
        return s

other = notNone(s, "default value")

Note, though, that since that's a function, it won't short-circuit like the conditional operator would if the first value isn't None; instead, it will evaluate both arguments even through the second one doesn't end up being used. For example, if a is not None, notNone(a, get_some_value()) will still call get_some_value, but a if a is not None else get_some_value() won't (operators can short-circuit, and the conditional operator does).

🌐
Python.org
discuss.python.org › ideas
Add None coalescing operator in Python - Ideas - Discussions on Python.org
April 19, 2021 - Just like in Javascript there is Nullish coalescing operator(??) for checking for null values, it would be a nice addition to python as python also hasNone . Currently users often check for None with if statement: if foo is not None: return ...
🌐
Python
peps.python.org › pep-0505
PEP 505 – None-aware operators | peps.python.org
JavaScript) [3] [4]. These operators provide syntactic sugar for common patterns involving null references. The “null-coalescing” operator is a binary operator that returns its left operand if it is not null.
other = s or "some default value"

Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean context. If the values are not boolean, they are converted to boolean for the purposes of the operator.

Note that the or operator does not return only True or False. Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false.

In this case, the expression x or y returns x if it is True or evaluates to true when converted to boolean. Otherwise, it returns y. For most cases, this will serve for the very same purpose of C♯'s null-coalescing operator, but keep in mind:

42    or "something"    # returns 42
0     or "something"    # returns "something"
None  or "something"    # returns "something"
False or "something"    # returns "something"
""    or "something"    # returns "something"

If you use your variable s to hold something that is either a reference to the instance of a class or None (as long as your class does not define members __bool__() and __len__()), it is secure to use the same semantics as the null-coalescing operator. NB. Python 2.7 will look for __nonzero__() instead of __bool__().

In fact, it may even be useful to have this side-effect of Python. Since you know what values evaluates to false, you can use this to trigger the default value without using None specifically (an error object, for example).

In some languages this behavior is referred to as the Elvis operator.

Answer from Juliano on Stack Overflow
🌐
LWN.net
lwn.net › Articles › 956862
The return of None-aware operators for Python [LWN.net]
The saga of the None-aware (or null-coalescing) operators for Python continues. We last looked in on the topic a little over a year ago and noted that either adoption or a clear rejection of the idea might help tamp down its regular recurrence.
🌐
Wikipedia
en.wikipedia.org › wiki › Null_coalescing_operator
Null coalescing operator - Wikipedia
October 31, 2025 - The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since version 7.0.0, Perl since version 5.10 as logical defined-or, ...
🌐
Python.org
discuss.python.org › ideas
Add None coalescing operator in Python - #34 by malemburg - Ideas - Discussions on Python.org
December 15, 2022 - Just like in Javascript there is Nullish coalescing operator(??) for checking for null values, it would be a nice addition to python as python also hasNone . Currently users often check for None with if statement: if f…
Find elsewhere
🌐
Python.org
discuss.python.org › ideas
Add None coalescing operator in Python - #38 by petersuter - Ideas - Discussions on Python.org
December 15, 2022 - Just like in Javascript there is Nullish coalescing operator(??) for checking for null values, it would be a nice addition to python as python also hasNone . Currently users often check for None with if statement: if f…
🌐
CSDN
devpress.csdn.net › python › 62fd451bc677032930803231.html
Is there a Python equivalent of the C# null-coalescing operator?_python_Mangs-Python
Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false. In this case, the expression x or y returns x if it is True or evaluates to true when converted to boolean.
🌐
Ed's Big Plans
eddiema.ca › 2010 › 07 › 07 › the-null-coalescing-operator-c-ruby-js-python
The Null Coalescing Operator (C#, Ruby, JS, Python) at Ed's Big Plans
April 8, 2018 - DummyNode a = null; DummyNode b = new DummyNode(); DummyNode c = new DummyNode(); return a ?? b; // returns b return b ?? a; // still returns b DummyNode z = a ?? b; // z gets b return a ?? new DummyNode(); // returns a new dummy node return null ?? a ?? null; // this code has no choice but to return null return a ?? b ?? c; // returns b -- the first item in the chain that wasn't null · No, you’d never really have a bunch of return statements in a row like that — they’re only there to demonstrate what you should expect. These languages are less straight forward (i.e. possess picky nuances) since they are happy to evaluate any objects of any class with their coalescing operators (including emulated primitives).
🌐
PyPI
pypi.org › project › pep505
Python PEP505
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Python.org
discuss.python.org › ideas
Add None coalescing operator in Python - Page 3 - Ideas - Discussions on Python.org
December 15, 2022 - Just like in Javascript there is Nullish coalescing operator(??) for checking for null values, it would be a nice addition to python as python also hasNone . Currently users often check for None with if statement: if f…
🌐
Python.org
discuss.python.org › ideas
Introducing a Safe Navigation Operator in Python - Page 3 - Ideas - Discussions on Python.org
October 9, 2023 - I’ve been considering the idea of proposing a new feature in Python - a Safe Navigation Operator, similar to what’s available in languages like JavaScript, Ruby, and C#. Before proceeding with writing a formal PEP, I wan…
🌐
Google Groups
groups.google.com › g › python-ideas › c › Wrf5TTAi2mQ
[Python-ideas] Null coalescing operator
Indeed, since it leverages Python exception mechanism, any lib implementing a clean error model can immediately let the users benefit from it without having to implement, tests and document lots of helper methods with "default" keywords and the like, while not being limited to a set of values some operators would only care about, such as None. Plus, since EAFP is a popular and handy pattern, it makes sense. At last, it has the same characteristic as the null coalescing operator: it's lazy, and hence has a small performance interest too compared to functional equivalent.
🌐
Python.org
discuss.python.org › ideas
Add None coalescing operator in Python - Page 2 - Ideas - Discussions on Python.org
December 14, 2022 - Just like in Javascript there is Nullish coalescing operator(??) for checking for null values, it would be a nice addition to python as python also hasNone . Currently users often check for None with if statement: if f…
🌐
Python.org
discuss.python.org › ideas
Add None coalescing operator in Python - #46 by steven.daprano - Ideas - Discussions on Python.org
December 16, 2022 - Just like in Javascript there is Nullish coalescing operator(??) for checking for null values, it would be a nice addition to python as python also hasNone . Currently users often check for None with if statement: if f…
🌐
Readthedocs
glom.readthedocs.io › en › latest › faq.html
Frequently Asked Questions — glom 25.12.0 documentation
Null coalescing operators traverse nested objects and return null (or None for us) on the first null or non-traversable object, depending on implementation.
🌐
Stack Overflow
stackoverflow.com › questions › 68227722 › is-there-a-operator-for-python
Is there a ?? operator for python - Stack Overflow
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?
🌐
Python.org
discuss.python.org › peps
Revisiting PEP 505 – None-aware operators - PEPs - Discussions ...
September 19, 2024 - All, I would like to revive PEP 505, which proposes null-coalescing via ?., ??, and ??=. I believe this set of operators fills an ergonomic gap in the language where users are currently forced to choose between the footguns associated with or and the verbosity of if else expressions.
🌐
Google Groups
groups.google.com › g › dev-python › c › 9U7xBW2yZQU
[Python-Dev] PEP 505 (None-aware operators) for Python 3.11
While I believe the existence and use of these operators in other languages definitely helps the case that these can be used and understood successfully, I think it is entirely valid to either consider other syntax (though I prefer the chosen syntax of PEP 505), or even to reduce PEP 505 to having only the coalesce operator (??) and the maybe-assign operator (??=). Separately, I have implemented a pure-Python solution for PEP505 (which is definitely rather beta) which might help test the waters for a final implementation in CPython (though the CPython implementation would of course be much more efficient).