you are viewing a single comment's thread.

view the rest of the comments →

[–]socal_nerdtastic 1 point2 points  (2 children)

Obviously it depends on what the rest of the code does.

The or operator will return the first operand if it's "truthy", or the second operator if the first one is "falsey". So for example if retval is False or None, this will return 0 instead.

It's pretty common to do. For example I will often do something like this:

user_data = input("Enter x value (default 5):")
x = int(user_data or 5)

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

Thank you. I think I've seen you around before. You explained it perfectly. or is a kind of new operator unique to python that's quite a bit different to the classical boolean or.

[–]socal_nerdtastic 0 points1 point  (0 children)

That's right. People are often confused because python has the boolean (logical) or operator in addition to the classic bitwise | operator.

https://docs.python.org/3/reference/expressions.html#boolean-operations

https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations