Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 766
Klaus Byskov Pedersen Avatar asked Feb 12 '11 15:02

Klaus Byskov Pedersen


People also ask

Can Python be used instead of C?

Ease of development – Python has fewer keywords and more free English language syntax whereas C is more difficult to write. Hence, if you want an easy development process go for Python. Performance – Python is slower than C as it takes significant CPU time for interpretation. So, speed-wise C is a better option.

What does C stand for in Python?

c command Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command). - anything afterward is passed as options to python script or command, not interpreted as an option to interpreter itself.

Is C and C++ similar to Python?

Python and C++ are both general-purpose programming languages. Both languages are pretty different when it comes to overall approaches like syntax, usage, etc. But still, it gets difficult while you select the correct language.


2 Answers

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.

like image 177
Juliano Avatar answered Sep 27 '22 20:09

Juliano


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") 
like image 26
Hugh Bothwell Avatar answered Sep 27 '22 21:09

Hugh Bothwell