-2

I am just wondering if there is a way to assign a value/alternate value when it is None as below

conventional way

result = a if a else b

Expected way

result = assign(a,b)

PS: I could create a custom function but I would like to know if there is something by default already

newbie
  • 1,282
  • 3
  • 20
  • 43
  • what is wrong with res = a_default if a is None else set_a? – Mathieu Oct 22 '20 at 13:05
  • 3
    Does this answer your question? [Is there a Python equivalent of the C# null-coalescing operator?](https://stackoverflow.com/questions/4978738/is-there-a-python-equivalent-of-the-c-sharp-null-coalescing-operator) – Guy Incognito Oct 22 '20 at 13:06

2 Answers2

2
    a = None
    b = "Hi"
    result = a or b
    print(result) # prints "Hi"

    a = "Hello"
    b = "Hi"
    result = a or b
    print(result) # prints "Hello"
Majo
  • 56
  • 4
1
result = a or b

If a is none, result will be b

mzndr
  • 91
  • 8