|
|
|
|
|
by dec0dedab0de
1795 days ago
|
|
EDIT2: What I was replying to is no longer there. It does short circuit, your logic is just purposely fragile. If you reverse the variables it will print Empty, because the and statement evaluates left to right and stops if one of the arguments is falsey. print(len(foo) > 0 and foo[0] or "Empty")
And since len can't give negative numbers the more Pythonic way to do it would be like this. Even though I generally prefer normal if blocks. print(len(foo) and foo[0] or "Empty")
edit: I do agree that you should probably avoid doing this at all, because it is easy to introduce subtle bugs. |
|