# Conditional Expresion/Statements

**URL:** <https://discuss.python.org/t/conditional-expresion-statements/25193>\
**Category:** Ideas\
**Created:** [March 26, 2023, 12:34pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193 "2023-03-26T12:34:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 12:34pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/1 "2023-03-26T12:34:02Z")

</div>

Quite often when coding I feel like this would be a very logical way to express things

```python
retries -= 1 if logical_condition
retries = function_call() if logical_condition
return if logical_condition
return None if logical condition

```

Obviously this can be seen as nothing new given that both can be written as

```python
if logical_condition:
    retries -= 1

if logical_condition:
    retries = function_call()

if logical_condition:
    return

if logical_condition:
    return None

```

It is probably because the former can be read in a natural manner like: “return if something\_is\_True”

Is this an avenue which would be interesting exploring?

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [March 26, 2023, 1:01pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/2 "2023-03-26T13:01:38Z")

</div>

Almost certainly not. It’s similar to the Perl “conditional modifier” syntax, which was a key factor in Perl’s “there’s more than one way to do it” style. Python consciously adopted a “there should be one obvious way to do it” style (see the Zen of Python for the exact wording), partly in reaction to Perl’s approach.

So your proposal is going against a pretty fundamental design principle of Python. Times have changed, and it’s possible that people might be more receptive to an idea like this now, if there was a compelling enough case for it, but honestly, I doubt it. If you want to pursue the idea, you should probably search the archives both here and on the old mailing list, to understand what specific reasons people gave for rejecting this proposal in the past (it has come up before), so that you can address those points.

But personally, I’m -1 on the proposal. Yes, it would be convenient at times, but I value consistency and regularity more than occasional convenience, as a general principle.

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [March 26, 2023, 1:10pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/3 "2023-03-26T13:10:43Z")

</div>

We do already have a terse syntax for these one-liners:

```auto
>>> retries = 2
>>> if True: retries -= 1
...
>>> retries
1

```

Admittedly, most auto-formatters will reformat it…

---

<div class="post-metadata">

**Author:** ![a-reich](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/a-reich/32/5885_2.png) [@a-reich](https://discuss.python.org/u/a-reich)\
**Post date:** [March 26, 2023, 1:54pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/4 "2023-03-26T13:54:34Z")

</div>

Or ternary expressions:

```auto
retries = 2
retries = retries - 1 if logical_condition else retries

```

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 1:56pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/5 "2023-03-26T13:56:41Z")

</div>

The point is not whether it can be a one liner when read left to right. The point would be if reading it from right to left, which (at least to me) seems more natural would make sense, eliminating also the need for the `:`

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 1:58pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/6 "2023-03-26T13:58:38Z")

</div>

Ternary expressions are almost there, the issue at hand there that the “idea” would avoid the execution of the 2nd part (because it doesn’t exist) which is: `retries = retries`

Exactly this type of redundant behavior (which I typed over and over again) is what led me in the first place to think that a more compact syntax could actually make sense

---

<div class="post-metadata">

**Author:** ![notatallshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/notatallshaw/32/10861_2.png) [@notatallshaw](https://discuss.python.org/u/notatallshaw)\
**Post date:** [March 26, 2023, 2:06pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/7 "2023-03-26T14:06:31Z")

</div>

> [@a-reich](#):
>
> ```auto
> retries = 2
> retries = retries - 1 if logical_condition else retries
> 
> ```

> [@mementum](#):
>
> Ternary expressions are almost there, the issue at hand there that the “idea” would avoid the execution of the 2nd part (because it doesn’t exist) which is: `retries = retries`

Well it can be done shorter without the “redundant” `retries`:

```python
retries = 2
retries -= 1 if logical_condition else 0

```

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 2:18pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/8 "2023-03-26T14:18:15Z")

</div>

You still have something redundant, which doesn’t add to clarity and generates something which is executing if the logical\_condition is not met

```python
retries -= 0 # from retries -= 1 if False else 0

```

See, I understand that things can already be done with a ternary operator, but the idea is to drop the “third” part and avoid a pointless execution in the 1st place.

Whilst making the statement/expression/call-it-x a bit more natural (imho)

```python
retries -= 1 if logical_condition

```

which should be read as: `retries -= 1` is only going to be executed if: `if logical_condition evaluates to True`

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 2:25pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/9 "2023-03-26T14:25:12Z")

</div>

When I started with Python there were no PEPs (the idea still needed a couple of years) and the idea of the Zen of Python didn’t even exist either. That Guido used the principles for the design and development of Python is out of the question.

(That I started so early makes me not a foremost Python expert, because I am not, it is just a historical reference)

I also like very much the “one obvious way” to do things but I think that train is long gone, which at the same type doesn’t mean one has to add all to the language.

The proposal simply extra-develops the ternary operator by letting one drop the “else” part and with it avoiding the execution of the statement/expression bound to the dernary-operator now.

It is nothing different (as expressed in the OP) from writing it the other way round

```python
if logical_condition:
    retries -= 1

```

It’s about whether the natural language approach and thinking when confronted with

```python
retries -= 1 if logical_condition

```

makes sense.

---

<div class="post-metadata">

**Author:** ![notatallshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/notatallshaw/32/10861_2.png) [@notatallshaw](https://discuss.python.org/u/notatallshaw)\
**Post date:** [March 26, 2023, 2:37pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/10 "2023-03-26T14:37:50Z")

</div>

> [@mementum](#):
>
> You still have something redundant, which doesn’t add to clarity and generates something which is executing if the logical\_condition is not met

I believe this is subjective, it adds clarity on what to do when `logical_condition` isn’t met.

So currently there are two options:

1. Syntax that achieves exactly semantically and runtime what you want but in two lines rather than one line
2. Syntax that is _very_ close to what you want and can be done in one line

So you must justify why a third option needs to be created here.

I would look at the old discussions on the walrus operator which introduced an expression syntax to an existing statement syntax, it was not introduced without **significant** debate and justification, e.g. [Mailman 3 PEP 572: Assignment Expressions - Python-Dev - python.org](https://mail.python.org/archives/list/python-dev@python.org/thread/DYR74OQKFTNEWIEK55EG5KMJQUKFWRV4/#DYR74OQKFTNEWIEK55EG5KMJQUKFWRV4) (there were many more threads in python dev and python ideas, this was just the easiest to find)

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [March 26, 2023, 2:42pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/11 "2023-03-26T14:42:15Z")

</div>

> [@mementum](#):
>
> When I started with Python there were no PEPs (the idea still needed a couple of years) and the idea of the Zen of Python didn’t even exist either. That Guido used the principles for the design and development of Python is out of the question.

That’s not what I meant - the Zen (as you know) codified the principles that were already in use. Sorry for assuming you didn’t know the history, I was also around back then, but I tend to assume many of the proposals we see are from newcomers to the language.

As I said, if you want to pursue this, it’s worth a try, but I think you’ll need a better argument than simple convenience.

> [@mementum](#):
>
> It’s about whether the natural language approach and thinking when confronted with
> 
> ```auto
> retries -= 1 if logical_condition
> 
> ```
> 
> makes sense.

You’d also need to consider the fact that

```auto
retries -= 1 if logical_condition else 2

```

is currently valid syntax, and both the parser and human readers\[1\] need to look ahead quite a long way to distinguish the two cases, and that often hurts readability. Particularly if `logical_condition` is a complex expression (which it typically is, from my experience with Perl, as that’s when you want to push it to the end, to avoid having it “hide” the main point of the construct, which is to decrement `retries`).

Maybe you should identify some examples, from public code like the stdlib or 3rd party libraries, where this proposal would improve readability. For completeness (and contrast), you could add some examples of where using it would be a _bad_ idea, and therefore get a feel of how often this would be a useful thing to have.

* * *

1. I was going to say “especially non-English speakers”, but I don’t want to make unwarranted assumptions again, and I personally only speak English so my experience is limited to that.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [March 26, 2023, 3:07pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/12 "2023-03-26T15:07:54Z")

</div>

When I learned Python, it was a simple but still expressive programming language (comparing with Awk, Perl, Ruby, Tcl). A typical programmer could grasp the basics in half a day and start writing simple scripts. Then finish reading the official tutorial and consider themself a pro. The rest came with experience.

Now there are thousands of online courses and couches. Maybe it’s time to move away from the old principles and add unrestricted complexity to the syntax. Maybe this is what the next generation needs.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [March 26, 2023, 3:08pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/13 "2023-03-26T15:08:20Z")

</div>

> [@mementum](#):
>
> ```auto
> retries -= 1 if logical_condition
> 
> ```

I dislike this because it makes it difficult to tell at a glance whether a variable is defined in a block of code. Currently, if a line starts with `retries =`, you know that later in the same block, that variable will definitely exist. With this proposed syntax, you have to scan for whether there’s an `if` somewhere in the middle of the expression.

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 3:27pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/14 "2023-03-26T15:27:19Z")

</div>

> I was also around back then

Hong Kong Phooey is for sure testimony to that. I also watched it as a kid.

In any case, you may consider (I do) me a beginner, I have never worked professionally as a programmer.

Back in business.

> Maybe you should identify some examples, from public code like the stdlib or 3rd party libraries, where this proposal would improve readability. For completeness (and contrast), you could add some examples of where using it would be a _bad_ idea, and therefore get a feel of how often this would be a useful thing to have.

I had `threading.py` open and had the ` __init__ ` method of `Condition` at hand.

Original

```python
    def __init__ (self, lock=None):
        if lock is None:
            lock = RLock()
        self._lock = lock
        # Export the lock's acquire() and release() methods
        self.acquire = lock.acquire
        self.release = lock.release
        # If the lock defines _release_save() and/or _acquire_restore(),
        # these override the default implementations (which just call
        # release() and acquire() on the lock). Ditto for _is_owned().
        if hasattr(lock, '_release_save'):
            self._release_save = lock._release_save
        if hasattr(lock, '_acquire_restore'):
            self._acquire_restore = lock._acquire_restore
        if hasattr(lock, '_is_owned'):
            self._is_owned = lock._is_owned
        self._waiters = _deque()

```

Syntax-modified

```python
    def __init__ (self, lock=None):
        self._lock = lock = RLock() if lock is None

        # Export the lock's acquire() and release() methods
        self.acquire = lock.acquire
        self.release = lock.release

        # If the lock defines _release_save() and/or _acquire_restore(),
        # these override the default implementations (which just call
        # release() and acquire() on the lock). Ditto for _is_owned().
        self._release_save = lock._release_save if hasattr(lock, '_release_save')
        self._acquire_restore = lock._acquire_restore if hasattr(lock, '_acquire_restore')
        self._is_owned = lock._is_owned if hasattr(lock, '_is_owned')
        self._waiters = _deque()

```

I don’t know how other readers see it, but I look at both versions and the 2nd one seems a lot cleaner and easier to understand. The number of lines is of course not relevant.

Even the assignment `self._lock = lock` can now be re-written to be part of the 1st statement.

> You’d also need to consider the fact that
> 
> ```auto
> retries -= 1 if logical_condition else 2
> 
> ```
> 
> is currently valid syntax, and both the parser and human readers need to look ahead quite a long way to distinguish the two cases, and that often hurts readability. Particularly if `logical_condition` is a complex expression (which it typically is, from my experience with Perl, as that’s when you want to push it to the end, to avoid having it “hide” the main point of the construct, which is to decrement `retries`).

Considered from the very beginning of the idea. I do actually (and truly) believe that it is an extension of that syntax, which allows dropping the `else 2` (in the example)

The ternary assigment without `else` (call it _"dernary_" for marketing purposes) is what kept banging in my head whenever I felt that a `return` following an `if` could be read a lot more naturally by writing first the `return` and then the logical condition to be evaluated, which would determine the execution of the `return`.

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 3:33pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/15 "2023-03-26T15:33:54Z")

</div>

> > [@mementum](#):
> >
> > ```auto
> > retries -= 1 if logical_condition
> > 
> > ```
> 
> I dislike this because it makes it difficult to tell at a glance whether a variable is defined in a block of code. Currently, if a line starts with `retries =`, you know that later in the same block, that variable will definitely exist. With this proposed syntax, you have to scan for whether there’s an `if` somewhere in the middle of the expression.

In that particular example: `retries` **must** already exist or else the `-=` will already generate an exception.

This other example is different:

```python
retries = 0 if logical_condition

```

Indeed: the variable may or may not be defined by the expression. In this case, the AST should “conditionally” define the variable. If upon exiting the scope in which the variable exists the variable is used with no other fixed definition (ex-ante, ex-post) having taking place.

For `return` (and other) statements the logic is a lot clearer. It is only executed if the `logical_condition` evaluates to `True`

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 26, 2023, 3:46pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/16 "2023-03-26T15:46:25Z")

</div>

> [@mementum](#):
>
> This other example is different:
> 
> ```auto
> retries = 0 if logical_condition
> 
> ```
> 
> Indeed: the variable may or may not be defined by the expression. In this case, the AST should “conditionally” define the variable. If upon exiting the scope in which the variable exists the variable is used with no other fixed definition (ex-ante, ex-post) having taking place.

I would assume that it’s semantically equivalent to `if logical_condition: retries = 0` - is that not the case?

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 26, 2023, 5:02pm UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/17 "2023-03-26T17:02:36Z")

</div>

That’s all explained in the initial post. Whether it is a one-liner or not it’s not the point. It’s not about one-liners, it’s about turning around how the syntax is read to make it more natural.

For the sake of it:

```python
return if not q.qsize()

```

Which would only execute the `return` if the size of a `queue.Queue` (or something supporting a similar API) is zero.

Yes it can also be written as:

```python
if not q.qsize():
    return

```

(which can of course be a one-liner even if not PEP-8 compliant and formatters will reformat it)

but after years of typing the latter, my python-soul has the impression that writing the former would be more natural, expressive and clearer.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 27, 2023, 12:43am UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/18 "2023-03-27T00:43:50Z")

</div>

For many years I have defended Python’s ternary operator `value if condition else alternative` on the basis that this is very close to the English

“I’m going to the movies tonight, if I get home early, otherwise I’ll just watch a DVD.”

I still stand by that. English does allow the conditional “if” clause to appear second.

But I don’t think even the most enthusiastic defenders of ternary-if say that this form is _the most natural_ format, only that it isn’t as awful as the opponents pretend.

There is a good reason why, out of hundreds of programming languages in the world, only a handful mess with the standard `if condition then A else B` syntax – and most of those are concatenative languages using Reverse Polish Notation with a stack, like Forth and Factor. Failing a good reason to change the order, the most natural order is to put the decision first, then the alternatives.

(At least for languages based on English-like grammar. Other natural languages may differ.)

If you think that `result = expression if condition` is more readable and natural than `if condition: result = expression`, I think that this is an idiosyncratic preference that very few people will share.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 27, 2023, 12:50am UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/19 "2023-03-27T00:50:35Z")

</div>

There are also practical issues with this suggestion:

Is this an expression or a statement?

If it is an expression then it has to have a value regardless of whether the condition is true or not:

```
print(("Hello" if condition).upper(), "WORLD")

```

will print “HELLO WORLD” if the condition is true, but what will it print if it is false? Expressions can’t just evaluate to nothing at all.

If it is a statement, we will need to modify the grammar rules to allow an optional if condition. So we have to work out where it is allowed. Some are obvious:

```
return expression [if condition] #	[...] means optional
expression [if condition]
result = expression [if condition]
result += expression [if condition] # and likewise for other augmented assignments

```

But where else might it go?

```
import name [if condition]
del namelist [if condition]

```

not to mention weirder possibilities:

```
try [if condition] :
    block
except ...

def function_name(parameterlist) [-> return_annotation] [if condition] :
    block

for targetlist in iterable [if condition] :
    block

```

etc. Note that the last example conflicts with another often-requested feature, “Let’s save one line and an indent by allowing if with for loops like in comprehensions”.

```
for x in sequence if cond: ...

```

could be syntactic sugar for _either_ of these:

```
if cond:
    for x in sequence: ...

for x in sequence:
    if cond: ...

```

Either way, all of these conditionals require the grammar rules to be updated to allow an optional if.

Or… we can just stick to the status quo, and _compose_ statements. As you acknowledge, this does nothing to the language that we don’t already have by composing an `if` statement with whatever you want. It literally adds zero new functionality to Python and allows us to do nothing we couldn’t already do.

This discussion is purely an argument about the colour of the bikeshed: should we keep the existing colour `if condition: code` or can we paint it the fancy new colour `code if condition`?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 27, 2023, 2:22am UTC](https://discuss.python.org/t/conditional-expresion-statements/25193/20 "2023-03-27T02:22:02Z")

</div>

> [@steven.daprano](#):
>
> There are also practical issues with this suggestion:
> 
> Is this an expression or a statement?

It’d definitely be a statement, although one form of simple statement is the expression statement, so for instance `print(...) if debug` would be fully valid (the statement happens to be an expression).

The usual reason for having this sort of thing in a language is that it puts the **action** front and center, instead of the condition. A common example is this sort of “if debug” check, where you would put the focus on the actual output call, and only as a minor footnote, show whether it will happen or not. It’s not just about the colour of your bikeshed; it’s like everything else where there are multiple spellings, and the programmer is allowed to emphasize the important parts.

Personally, I don’t think it’s necessary, and I also don’t much like it; but that’s a matter of opinion.

[Next page](https://discuss.python.org/t/conditional-expresion-statements/25193.md?page=2)
