-2

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

if completion.is_anonymous:
    user = 'Anonymous'
else:
    user = completion.user
David542
  • 104,438
  • 178
  • 489
  • 842
  • 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
  • 19,907
  • 5
  • 54
  • 57
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
  • 15,966
  • 8
  • 49
  • 55