-2

Is there a way to do the following on one line?

if completion.is_anonymous:
    user = 'Anonymous'
else:
    user = completion.user
David542
  • 101,766
  • 154
  • 423
  • 727
  • 1
    It surprises me that you have ~9k reputation but the thought of googling this simple task didn't cross your mind – Tim Oct 30 '14 at 16:43

2 Answers2

3

Use ternary operators:

user = 'Anonymous' if completion.is_anonymous else completion.user
anon582847382
  • 18,791
  • 5
  • 51
  • 56
0

There is the user = "Anonymous" if completion.is_anonymous else completion.user syntax:

>>> a = 2 if True else 4
>>> a
2
>>> a = 2 if False else 4
>>> a
4
fredtantini
  • 14,608
  • 7
  • 46
  • 54