2

Lets say we have the following:

my_condition = True
print('AAA' if my_condition else 'BBB')

The output would be:

AAA


Conversely, if my_condition became False:

my_condition = False
print('AAA' if my_condition else 'BBB')

The output would be:

BBB


Now, lets say I wanted to put that same if my_condition else 'BBB' at the end of a bunch of different print functions.

Is there a way to alias that statement?

So instead of:

my_condition = True
print('AAA' if my_condition else 'BBB')

I wanted something like this:

myStatement = if my_condition else 'BBB'    

my_condition = True
print('AAA' myStatement)

Except, as I've already discovered, this doesn't work. Is it possible to alias this If-Else statement?

Edit I should have mentioned that I'm running this on Python 2.7, however I do appreciate the 3.X solutions.

holaymolay
  • 520
  • 4
  • 17
  • Not exactly, no. But you can write a function that does that and then call it. – BrenBarn Feb 19 '14 at 07:50
  • Yes, the condition will always be the same. I was trying to implement a debugging feature into my program, and I didn't certain print functions to show unless i had debugging was enabled. – holaymolay Feb 19 '14 at 08:23

3 Answers3

5

Use a lambda expression

y = lambda x: x if my_condition else 'BBB'
print(y('AAA'))
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
2

It seems you want a function, there are many ways of getting the result you want.

This one is very similar to what you've tried:

foo = lambda x: x if condition else "BBB"
print (foo("AAA"))

It's a lambda expression.

Of course you could always go with the good old fashioned function:

def foo(x):
    return x if condition else "BBB"

And another option you have (this would be my favourite):

Let's say you have a list with that bunch of words you want to print, why write than many prints? You could be using list comprehensions

foo = ["hola","que","tal"];

[print(x if my_condition else "BBB") for x in foo]

Also if you don´t want "BBB" to be always the "else" option:

foo = [("hola","pues"),("que","nada"),("tal","bien")]

[print(x if my_condition else y) for (x,y) in foo]

Note this won't work in Python 2.X, since print in 2.X is a statement and not a function.

Community
  • 1
  • 1
JJGuerrero
  • 519
  • 3
  • 8
  • Ups! Didn't notice that you don't want all the prints at the same time on the same place, so list comprehesions don't suit your needs, sorry. – JJGuerrero Feb 19 '14 at 09:02
  • Okay, this is really good. I tried to implement the `foo = lambda x: x if condition else "BBB" print (foo("AAA"))` but, as you predicted, it doesn't work for me in 2.X. Would there happen to be a combined 2.X/3.X solution? – holaymolay Feb 19 '14 at 09:16
  • That piece of code should work no matter what version you use, I meant the list comprehensions (sorry, it wasn´t clearly specified) What is the error you are getting? Anyway to make it work (in list comprehensions, as I said, the lambda expression should be working) you may use `from __future__ import print_function` to get print as a function or you could simply `import sys` and use `sys.stdout.write('hola')` – JJGuerrero Feb 19 '14 at 09:58
  • Yeah, I've tried all that. It just didn't work for me. It was okay about displaying "AAA" but the else condition just didn't work at all in 2.X. I trust that it works on 3.X, but my code isn't 3.X compatible yet. – holaymolay Feb 21 '14 at 02:37
1

If my_condition is always an actual bool, you could do something like

>>> base_case = ["BBB"]
>>> my_condition = True
>>> print((base_case + ["AAA"])[my_condition])
AAA
>>> my_condition = False
>>> print((base_case + ["AAA"])[my_condition])
BBB

but I would advise against it since it looks very unpythonic to me.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561