This is a very subjective question, but I'm curious as to other people's opinions.
Would you find this bit of code distasteful? Is it cryptic and unreadable, or is it using features of the language in an elegant way?
def winning_side(home_goals, away_goals):
"""Takes a home score and an away score, and returns the winner
Return DRAW, HOME or AWAY depending on the outcome
"""
return (DRAW, HOME, AWAY)[cmp(home_goals, away_goals)]
The alternative obviously being explicit if-else statements:
def winning_side(home_goals, away_goals):
"""Takes a home score and an away score, and returns the winner
Return DRAW, HOME or AWAY depending on the outcome
"""
if home_goals > away_goals:
return HOME
elif away_goals > home_goals:
return AWAY
else:
return DRAW
I personally find the verbose if-else logic in the latter solution, and the assumed equality in the 3rd branch, less appealing.
cmp
falls to the clever category. \$\endgroup\$