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

16

531

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

Diagonal answered 12/2, 2011 at 15:4 Comment(3)
The ?? operator is proposed as PEP 505.Perfusion
..but never made it into the language.Spiculate
One of the biggest strengths of Python is its expressiveness. It's a pity Python doesn't provide a None-coalescing operator. The ternary alternative is way more verbose and the or solution is simply not the same (as it handles all "falsy" values, not just None - that's not always what you'd want and can be more error-prone).Rheo
S
696
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 __nonzero__() and __len__()), it is secure to use the same semantics as the null-coalescing operator.

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.

Sisterly answered 12/2, 2011 at 15:6 Comment(28)
Will this work the same? I mean, will it break if s is a valid value but isn't truthy? (I don't know Python, so i'm not sure whether the concept of 'truthy' applies.)Telephony
The number 0, None, and empty containers (including strings) are considered false, in addition to the constant False. Most everything else is considered true. I would say that the main danger here would be that you would get a true but non-string value, but that won't be an issue in some programs.Nonconformity
Using this other will get the default value if s is None or False, which may not be what is wanted.Cornwell
This technique works even works with default values that evaluate to False, e.g. {}: data = None; data = data or {}; data is now set to {}.Beverlee
There are many obscure bugs caused by this as well. For example prior to Python 3.5, datetime.time(0) was also falsy!Refresher
'' or None would return None - skipping the first False value.Reiners
This is actually my favorite answer I've ever encountered on SE! Note that if you or 2 (or n) different falsey values, you get the last one as the result. i.e. '' or None evaluates to None and None or '' evaluates to ''. (So it's not symmetric for falsey values. But it should be symmetric for truthy values, except perhaps obscure corner cases.)Malo
This is bad. I recommend adding a notice about its pitfalls. And recommending not to use it.Heribertoheringer
Looks like the answer is missing the point of the original question. The value of ?? comes in when you do a??.calculate(). It works like the maybe monad in Haskell. I am no python expert, but still dont think the or operator solves the fundamental issueColes
@MateenUlhaq : You have the rep, why not edit the answer to add this?Nuptial
@PeterK The edit would "conflict with the author's intent".Heribertoheringer
@MateenUlhaq OK. Your comment seems superfluous in that case.Nuptial
Please do not do this. This answer is not equivalent to the C# null-coalescing operator, since the C# ?? operator responds specifically to whether the first operand is None/null and the Python or responds to anything that resolves to False when treated as a Boolean. That "resolves to False" has many subtle cases, and will likely introduce hard-to-find bugs in the future. Hugh Bothwell's answer is much more obviously correct.Deity
Can I apply this to command line args. Exmaple: arg = argv[1] or 1.Swound
So they are not exactly the same, because in C#, ((bool?) false ?? true) evaluates to False, but in Python, False or True evaluates to TrueDoorstone
Consider x()?.y()?.z()Teheran
⚠️ This is an antipattern. Use other = "some default value" if s is None else s. See also: #13711131Elkeelkhound
Not a good solution. Remember that Explicit is better than implicitGrueling
This is a terrible answer, and it got cited on the Wikipedia article on Null coalescing operator. The correct answer is "<h1>No.</h1><br>However ...(and what you wrote here)".Nitid
Undesirable behavior if asker only wants to test for None this overrides anything falsy 0 or "default" will default.Onomatopoeia
@np8 What if s is an expression?Eucalyptol
@Grueling Are you really talking about type system?Dike
How does such an incorrect and awful answer have so many upvotes?Putscher
Mark - because Python doesn't have a coalesce operator. I'm kinda surprised at the vitriol considering Python seems to want verbosity in an environment where people want conciseness. I'm about to use the OR method in my code - don't hate me bro.Sachasachem
What to do about np.nan or 'test'?Cleanly
What if the default value is falsy e.g. ""?Unlay
I hate that feature of Python. x or y should be equivalent to bool(x) or bool(y). The fact that it's behaving as True if bool(x) else y is bad.Kithara
Been using Python for 11 years and I did not realize or could do this!!! Awesome tip.Muscarine
E
163

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

Equiponderate answered 12/2, 2011 at 15:52 Comment(3)
This should be the answerTuinenga
I wonder what to do if my case is other = s.name if s is not None else "default value", is there also a shorter way like notNone?Didymium
This should be the answer and Python could consider adding such a convenience operatorEpizoon
M
66

Here's a function that will return the first argument that isn't None:

def coalesce(*arg):
  return reduce(lambda x, y: x if x is not None else y, arg)

# Prints "banana"
print coalesce(None, "banana", "phone", None)

reduce() might needlessly iterate over all the arguments even if the first argument is not None, so you can also use this version:

def coalesce(*arg):
  for el in arg:
    if el is not None:
      return el
  return None
Marmalade answered 27/4, 2013 at 0:45 Comment(5)
def coalesce(*arg): return next((a for a in arg if a is not None), None) does the same as your last example in one line.Santiagosantillan
I get that people want to explain if else sytnax etc, but coalesce takes an arbitrary argument list so this should really be the top answer.Nazarite
glglgl has the best answer. I used timeit on a large test array and the reduce implementation is unacceptably slow, the multi-line for/if version is fastest, and the next implementation is very slightly behind. The next version is the best overall when considering simplicity and conciseness.Roustabout
@Santiagosantillan has interesting snippet. Unfortunately because Python does not have pass-by-name, coalesce like this is not short-circuiting; all of the arguments are evaluated before the code runs.Provision
This works for me, but as of Python 3 (Python 2 is now EOL), from functools import reduce is required. Also, print is now a function that returns None, and not a statement.Ellisellison
T
41

In case you need to chain more than one null-conditional operation such as:

model?.data()?.first()

This is not a problem easily solved with or. It also cannot be solved with .get() which requires a dictionary type or similar (and cannot be nested anyway) or getattr() which will throw an exception when NoneType doesn't have the attribute.

The relevant PEP considering adding null coalescing to the language is PEP 505 and the discussion relevant to the document is in the python-ideas thread.

Teheran answered 24/4, 2020 at 11:18 Comment(5)
There's no null-coalescing operator in model?.data()?.first(). This answer isn't relevant to the question.Symbology
@Symbology did you read past that line?Teheran
^ it threw me off at first too - though if I'm being pedantic, that's not null coalescing in other languages either. It's C#'s null-conditional operator or typescripts optional chainingGraveyard
@Graveyard okay, fixedTeheran
it's good it's here because I had no idea what that operator was calledAmy
M
18

I realize this is answered, but there is another option when you're dealing with dict-like objects.

If you have an object that might be:

{
   name: {
      first: "John",
      last: "Doe"
   }
}

You can use:

obj.get(property_name, value_if_null)

Like:

obj.get("name", {}).get("first", "Name is missing") 

By adding {} as the default value, if "name" is missing, an empty object is returned and passed through to the next get. This is similar to null-safe-navigation in C#, which would be like obj?.name?.first.

Methadone answered 13/7, 2018 at 12:32 Comment(3)
Not all objects have .get, this only works for dict-like objectsVinson
I'm submitting an answer edit to cover getattr() also.Slavery
get on dict does not use the default parameter if the value is None but uses the default parameter if the value does not exist because the key is not in the dict. {'a': None}.get('a', 'I do not want None') will still give you None as a result.Macaulay
A
11

Addionally to @Bothwells answer (which I prefer) for single values, in order to null-checking assingment of function return values, you can use new walrus-operator (since python3.8):

def test():
    return

a = 2 if (x:= test()) is None else x

Thus, test function does not need to be evaluated two times (as in a = 2 if test() is None else test())

Attaway answered 2/3, 2020 at 13:3 Comment(1)
This is the cleanest way of achieving null-coalescing in my opinion. I use it for functions that can return the object or None, and want to access it on the same line; ie: email = user.email if (user := get_user_fn()) else NoneLangsyne
I
2

In addition to Juliano's answer about behavior of "or": it's "fast"

>>> 1 or 5/0
1

So sometimes it's might be a useful shortcut for things like

object = getCachedVersion() or getFromDB()
Immediacy answered 24/6, 2014 at 12:24 Comment(1)
The term you're looking for is "short-circuits."Leyva
C
0

Regarding answers by @Hugh Bothwell, @mortehu and @glglgl.

Setup Dataset for testing

import random

dataset = [random.randint(0,15) if random.random() > .6 else None for i in range(1000)]

Define implementations

def not_none(x, y=None):
    if x is None:
        return y
    return x

def coalesce1(*arg):
  return reduce(lambda x, y: x if x is not None else y, arg)

def coalesce2(*args):
    return next((i for i in args if i is not None), None)

Make test function

def test_func(dataset, func):
    default = 1
    for i in dataset:
        func(i, default)

Results on mac i7 @2.7Ghz using python 2.7

>>> %timeit test_func(dataset, not_none)
1000 loops, best of 3: 224 µs per loop

>>> %timeit test_func(dataset, coalesce1)
1000 loops, best of 3: 471 µs per loop

>>> %timeit test_func(dataset, coalesce2)
1000 loops, best of 3: 782 µs per loop

Clearly the not_none function answers the OP's question correctly and handles the "falsy" problem. It is also the fastest and easiest to read. If applying the logic in many places, it is clearly the best way to go.

If you have a problem where you want to find the 1st non-null value in a iterable, then @mortehu's response is the way to go. But it is a solution to a different problem than OP, although it can partially handle that case. It cannot take an iterable AND a default value. The last argument would be the default value returned, but then you wouldn't be passing in an iterable in that case as well as it isn't explicit that the last argument is a default to value.

You could then do below, but I'd still use not_null for the single value use case.

def coalesce(*args, **kwargs):
    default = kwargs.get('default')
    return next((a for a in arg if a is not None), default)
Corrugation answered 7/6, 2019 at 0:8 Comment(0)
D
0

to take care of possible exceptions:

def default_val(expr, default=None):
    try:
        tmp = expr()
    except Exception:
        tmp = default
    return tmp

use it like that:

default_val(lambda: some['complex'].expression('with', 'possible')['exceptions'], '')
Dismissal answered 23/11, 2022 at 7:22 Comment(0)
I
0

Give the opytional library a try.

>>> import opytional as opyt
>>> opyt.or_value('value', "some default value")
'value'
>>> opyt.or_value(None, "some default value")
'some default value'
>>> opyt.or_value("", "some default value")  # handles falsey value safely
''

The library also provides or_else and apply_if for safe, expressive handling of values that may be None. Installation is available via pip, python3 -m pip install opytional.

Disclaimer: am library author

Illuminance answered 25/12, 2023 at 7:33 Comment(0)
T
0

We can try like this

 other = s if s != None else 0 // to pass as 0 
 other = s if s != None else 'Some value in case of Null' // pass text 
Titanium answered 15/2 at 12:55 Comment(0)
S
0

I had a similar issue, but none of the solution above were quite satisfactory for me. I did some tests, then a nice idea came to my mind ... and it works !

it is at the same time elegant and 'descriptive' enough according to Python standards I think. I'll let you the judge of it.

First the issue:

I have a class Completion which has either a reference to sequence_type1 or sequence_type2

I want to define a property such as this:

@property
def seq_pk(self):
    return self.sequence_type1.seq_pk if self.sequence_type1 else self.sequence_type2.seq_pk
    

That's not really nice looking, takes a lot of space ... and I will need to do the same for all the properties of Completion !

I cannot simply do

return self.sequence_type1.seq_pk or self.sequence_type2.seq_pk

because if self.sequence_type1 does not exist, an exception will be raised, instead of giving me self.sequence_type2.seq_pk

But what I discovered works nicely is the following:

return (self.sequence_type1 or self.sequence_type2).seq_pk

simple, descriptive, elegant I think

Simdars answered 23/2 at 10:16 Comment(0)
F
0

For anyone still in need of a null coalescing for functions in 2024:

def null_coalesce(*args):
    return getattr(args[0], args[1], None) if len(args) == 2 else null_coalesce(getattr(args[0], args[1], None), *args[2:])

worked for me. This variant works if you want to access a field that may or may not exist. E.g. for foo.bar._name_ you would execute null_coalesce(foo, 'bar', '__name__') and would either receive the name of the function or if this field does not exist, you would get None and NOT an AttributeError.

Film answered 21/3 at 13:41 Comment(0)
I
-3
Python has a get function that its very useful to return a value of an existent key, if the key exist;
if not it will return a default value.

def main():
    names = ['Jack','Maria','Betsy','James','Jack']
    names_repeated = dict()
    default_value = 0

    for find_name in names:
        names_repeated[find_name] = names_repeated.get(find_name, default_value) + 1

if you cannot find the name inside the dictionary, it will return the default_value, if the name exist then it will add any existing value with 1.

hope this can help

Intestate answered 18/10, 2019 at 0:29 Comment(1)
Hi, welcome to Stack Overflow. What new information does your answer add which wasn't already covered by existing answers? See @Craig's answer for exampleYardarm
P
-3

For those like me that stumbled here looking for a viable solution to this issue, when the variable might be undefined, the closest i got is:

if 'variablename' in globals() and ((variablename or False) == True):
  print('variable exists and it\'s true')
else:
  print('variable doesn\'t exist, or it\'s false')

Note that a string is needed when checking in globals, but afterwards the actual variable is used when checking for value.

More on variable existence: How do I check if a variable exists?

Plath answered 9/1, 2020 at 16:14 Comment(1)
(variablename or False) == True is the same as variablename == TrueCheckbook
P
-6

The two functions below I have found to be very useful when dealing with many variable testing cases.

def nz(value, none_value, strict=True):
    ''' This function is named after an old VBA function. It returns a default
        value if the passed in value is None. If strict is False it will
        treat an empty string as None as well.

        example:
        x = None
        nz(x,"hello")
        --> "hello"
        nz(x,"")
        --> ""
        y = ""   
        nz(y,"hello")
        --> ""
        nz(y,"hello", False)
        --> "hello" '''

    if value is None and strict:
        return_val = none_value
    elif strict and value is not None:
        return_val = value
    elif not strict and not is_not_null(value):
        return_val = none_value
    else:
        return_val = value
    return return_val 

def is_not_null(value):
    ''' test for None and empty string '''
    return value is not None and len(str(value)) > 0
Puttyroot answered 14/3, 2016 at 12:37 Comment(1)
This kind of things adds a whole bunch of slightly different terminology (e.g. "null" and "nz" neither of which mean anything in the context of Python), imported from other languages, plus with variants (strict or non-strict!). This only adds confusion. Explicit "is None" checks are what you should be using. Plus you don't get the benefit of any short-cutting semantics that operators can do when you use a function call.Muttonchops

© 2022 - 2024 — McMap. All rights reserved.