print('something', ['a', 'list'][boolean])
Depending on the boolean value this either prints, a or list.
I have never seen this notation before and am wondering how it works.
bool is a subclass of int, where True is 1 and False is 0. isinstance(True, int) # True['a', 'list'][boolean] evaluates to ['a', 'list'][0] if boolean is False or to ['a', 'list'][1] if boolean is TrueThis can be abused by using conditions directly:
x = 1
print(['no', 'yes'][x > 0])
# yes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With