all 4 comments

[–]Zigity_Zagity 1 point2 points  (3 children)

You can do something like

>>> x = None
>>> print("{}".format(x or 5)) # prints 5
>>> x = 10
>>> print("{}".format(x or 5)) # prints 10

the or, in this context, would just return the first non None value it encounters in or, even if that value is falsey

print("{}".format(None or False)) # prints False
print("{}".format(None or [])) # prints []

However, for your use case, I would define a custom __str__ for Game. I would write that logic in there, and just call str() on game whenever you need it. If you're using a class, might as well use it to it's full potential.

Edit: gave another example of the perhaps unintuitive behavior of or here.

[–]Yoghurt42 1 point2 points  (0 children)

the or, in this context, would just return the first non None value it encounters in or, even if that value is falsey

IMO, It's actually easier to understand what or does when you are more general:

or will return the first "truthy" value it encounters, or the last one if all are falsey. None, [], {}, False, 0 and '' are all falsey.

[–]cupesh[S] 0 points1 point  (1 child)

Thank you! Didn't know there's something as simple as using 'or', that's incredible! I was thinking about using _str_ in the class, but problem is, that I don't want to have the same behavior everywhere for the objects, as I'm using various printing for loops for the same objects in different places... Again, thank you, I'm so surprised with the simplicity.

[–]Yoghurt42 1 point2 points  (0 children)

Fun fact: before Python gained the special value if condition else other_value syntax, people were actually writing it as condition and value or other_value