18

I tend to use this a lot, but it's ugly:

a = (lambda x: x if x else y)(get_something())

So I wrote this function:

def either(val, alt):
    if val:
        return val
    else:
        return alt

So you can do:

a = either(get_something(), y)

Is there a built-in function for this (similar to ISNULL in T-SQL)?

madth3
  • 7,275
  • 12
  • 50
  • 74
crizCraig
  • 8,487
  • 6
  • 54
  • 53

6 Answers6

39

The or operator does what you want:

get_something() or y

In fact, it's chainable, like COALESCE (and unlike ISNULL). The following expression evaluates to the left-most argument that converts to True.

A or B or C
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Thanks. Ha, I was actually trying that but dismissed it when trying 'a = 0 or None' and the console printed nothing. But trying 'a is None' after that gives 'True', plus I want 'None or 0' functionality anyway (getting late) :) By the way, thanks for the quick answer. – crizCraig Jul 15 '11 at 09:06
  • 3
    Just for the record, if you're to chain `and` operator, it will evaluate to right-most argument that converts to True if all arguments or the left-most argument that converts to False if any converts to False. – Michał Bentkowski Jul 15 '11 at 09:41
  • 1
    "a = 0 or None" Well of course the console won't print anything, you're assigning the result of `0 or None` to `a`, and variables with `None` assigned to them don't automatically display `None` when shown in the console. You have to specifically use `repr`, `str`, or `print`. Or something like that. – JAB Jul 15 '11 at 14:15
7

Easy!

For more conditional code:

a = b if b else val

For your code:

a = get_something() if get_something() else val

With that you can do complex conditions like this:

a = get_something() if get_something()/2!=0 else val
Phyo Arkar Lwin
  • 6,673
  • 12
  • 41
  • 55
6

You may use:

a = get_something() or y

If get_something is True in boolean context, its value will be assigned to a. Otherwise - y will be assigned to a.

Michał Bentkowski
  • 2,115
  • 16
  • 10
4

You can use a simple or, like so:

>>> a = None
>>> b = 1
>>> c = (a or b) # parentheses are optional
>>> c
1
miku
  • 181,842
  • 47
  • 306
  • 310
3

I have provided an answer to this question to another user. Check it out here:

Answer to similar question

To respond quickly here, do:

x = true_value if condition else false_value
Ghasem
  • 14,455
  • 21
  • 138
  • 171
mainas
  • 754
  • 1
  • 6
  • 15
1

I'm also using the (a,b)[condition based on the value of a] form, saving the result of the get_something() call into a, in the rare cases that are best presented here: http://mail.python.org/pipermail/python-list/2002-September/785515.html

...
a=0     b=None     a or b => None      (a,b)[a is None] => 0
a=()    b=None     a or b => None      (a,b)[a is None] => ()
...
alexandrul
  • 12,856
  • 13
  • 72
  • 99
  • I see. This would be if you wanted to check `a` for a more specific condition. But you need two lines so I would go with `a = x if condition else y` which is more readable. Interesting syntax though. Never seen anything like it :) – crizCraig Jul 15 '11 at 21:17
  • @crizCraig: added a short example – alexandrul Jul 15 '11 at 21:30