8

I'm a little curious about the difference between if and inline if, in Python. Which one is better?

Is there any reason to use inline if, other than the fact that it's shorter?

Also, is there anything wrong with this statement? I'm getting a syntax error: SyntaxError: can't assign to conditional expression

a = a*2 if b == 2 else a = a/w
vaultah
  • 44,105
  • 12
  • 114
  • 143
  • what do you want with the "a =" in the else clause? This has to be an expression and not an assignment. –  Oct 06 '13 at 10:19
  • So the 'inline if' you refer to is the *ternary operator* - and it fills a different role. It is only for assignment, not for general use. – Gareth Latty Oct 06 '13 at 10:21
  • "Which one is better" is a silly question. If one were always better than the other, Python wouldn't have both. The question is, _in what situations_ is each one better? – abarnert Oct 06 '13 at 10:28
  • I see. Thanks a lot! I thought both of it were statements. –  Oct 06 '13 at 10:30
  • 1
    @Lattyware: What do you mean "It is only for assignment"? I almost never use ternary expressions for assignment statements; I use them for function arguments, and occasionally list comprehensions or lambdas, where an `if `statement would do no good. – abarnert Oct 06 '13 at 10:31
  • @abarnert You are right, what I meant was that you should be *using* the value returned by the ternary operator (while function arguments and list comprehensions are not assignments exactly, they use that value and end up assigning it to something down the line (a parameter or a list element). It was a poor choice of words on my part. – Gareth Latty Oct 06 '13 at 10:38

3 Answers3

9

The advantage of the inline if expression is that it's an expression, which means you can use it inside other expressions—list comprehensions, lambda functions, etc.

The disadvantage of the inline if expression is also that it's an expression, which means you can't use any statements inside of it.


A perfect example of the disadvantage is exactly what's causing your error: a = a/w is a statement, so you can't use it inside an expression. You have to write this:

if b == 2:
    a = a*2
else:
    a = a/w

Except that in this particular case, you just want to assign something to a in either case, so you can just write this:

a = a*2 if b==2 else a/w

As for the advantage, consider this:

odd_numbers = [number if number%2 else number+1 for number in numbers]

Without the if expression, you'd have to wrap the conditional in a named function—which is a good thing for non-trivial cases, but overly verbose here:

def oddify(number):
    if number%2:
        return number
    else:
        return number+1
odd_numbers = [oddify(number) for number in numbers]

Also, note that the following example is not using an if (ternary conditional) expression, but an if (conditional filter) clause:

odd_numbers = [number for number in numbers if number % 2]
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 5
    It's worth noting that the ternary operator can be used inside a list comprehension, but the keyword `if` can also be used in a list comp for filtering. They are different things, so it's worth making the distinction between an `if` statement and the ternary operator. E.g: `["Something" if item > 10 else "SomethingElse" for item in data if item > 5]` - the first use of `if` is the ternary operator, the second is filtering in the list comp. – Gareth Latty Oct 06 '13 at 10:22
7

The correct way to use the conditional expression is:

result = X if C else Y

what you have is:

result = X if C else result = Y

So, you should remove the result = part from there. The major advantage of conditional expression is that, it's an expression. You can use them wherever you would use a normal expression, as RHS of assignment expression, as method/function arguments, in lambdas, in list comprehension, so on. However, you can't just put any arbitrary statements in them, like say print statements.

Fo e.g. suppose you want all even integers from a list, but for all odd numbers, you want the values as 0. You would use it in list comprehension like this:

result = [x if x % 2 == 0 else 0 for x in li]
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

Inline if is an expression, so you can not put assignments inside.

Correct syntax would be:

a = a*2 if b == 2 else a/w

As for the usefulness, it's a question of style, and perhaps it would be a good question for Programmers StackExchange.

Community
  • 1
  • 1
Denis
  • 5,061
  • 1
  • 20
  • 22