0

I would like to conditionally print a statement as such:

be_nice = True

print("Hello")
print("Good to see you!") if be_nice else pass

However, it seems that using pass here isn't allowed.

I found two ways to work around this, I can either do:

print("Good to see you!") if be_nice else 1

which seems to work since the 1 here simply does nothing, but is ugly. Or I can:

if be_nice: print("Good to see you!")

but this defeats the purpose of having the print's align with each other.

Is there a way to use a single line solution which has the print called at the start of the line?

  • 4
    You're complaining about some Pythonisms that I think you're just going to have to get used to. I'd argue you want the `if` to stand out as that is conditional logic and impacts the flow. There's nothing worse than thinking code will always run only to find some trailing `if` off-screen you need to scroll to see. – tadman Jun 01 '23 at 21:05
  • *"Is there a way to use a single line solution which has the print called at the start of the line?"* - Yes, you already showed one. – Kelly Bundy Jun 01 '23 at 21:10
  • @tadman So why is something like `my_var = 23 if some_long_condition else 42` possible? One may argue that this too can result in a long line with a trailing `if` off-screen and cause readability issues as you point out. – Fahrradkette Jun 01 '23 at 21:12
  • @KellyBundy Yes, but I am looking for something that does nothing and is as Pythonic as `pass` – Fahrradkette Jun 01 '23 at 21:14
  • Missed the last line of the question, it's indeed a bad idea to try to do what OP wants to do. – B Remmelzwaal Jun 01 '23 at 21:27
  • 2
    Using the ternary operator (`x if c else y`) for side effects (print) seems weird to me. I would stick with `if c: print(...)`. Worth noting that PEP 8 encourages you to put the print call on a new line, not the same line as the if statement (see https://peps.python.org/pep-0008/#other-recommendations). If you must use the ternary operator then using pass seems a sensible way to do it. – Andrew McClement Jun 01 '23 at 21:40
  • @KellyBundy When there's "surprise `if` conditions", equally, yes. – tadman Jun 01 '23 at 21:47
  • I'm just stating a preference, not a rule. I like to see branches laid out via indentation, though there are cases where an inline `if` works, like `[x for y in z if ...]` though this is mostly because you can spread this out over multiple lines for clarity. – tadman Jun 01 '23 at 21:48
  • @KellyBundy I think the question is clear enough. All I want is something similar to `print("Good to see you!") if be_nice else 1` with something that looks better than the `1` ... – Fahrradkette Jun 01 '23 at 22:15

3 Answers3

0

If you really want to do this, I suggest writing your own print function. Here are some "not terrible" options:

import functools as ft

be_nice = True

def print_if(cond, *args, **kwargs):
    if cond:
        print(*args, **kwargs)

print_if(be_nice, "Hello World")

print_if_be_nice = ft.partial(print_if, be_nice)
print_if_be_nice("Hello World")


def cond_print(*args, cond=False, **kwargs):
    if cond:
        print(*args, **kwargs)


cond_print("Hello World", cond=be_nice)
Kache
  • 15,647
  • 12
  • 51
  • 79
0

It look like, according what I was able to find in to the python documentation, that you use the only way to do it.

The documentation say :

The expression x if C else y first evaluates the condition, C rather than x. If C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

You can't use pass as y value because :

pass is a null operation — when it is executed, nothing happens.
Python pass documentation

pass is an operator so no value is return to evaluate x if C is equals to false.


If you want you can possibly use the Ellipsis object (...) as y value :

be_nice = True

print("Hello") print("Good to see you!") if be_nice else ... 

Effectively the Ellipsis is an object so a value. Here more information about the Ellipsis object.

Hope it helps you.

Cortard
  • 72
  • 5
0

You could also use:

print("Good to see you!\n" if be_nice else "", end="")
caulder
  • 83
  • 8