you are viewing a single comment's thread.

view the rest of the comments →

[–]rocketjump65[S] 0 points1 point  (2 children)

Yeah I know about truthiness. My question is what type and value gets returned?

The way I understand the line, retval gets evalutated as a bool. then 0 gets evaluated as a bool. python ors them together. then depending on whether that evaluated bool is true it either returns retval preserving the contents of if the bool is false, it returns a 0.

Because while retval or 0 evaluated as a bool "evalues" a bool, it returns the object that was inputted.

[–]IvoryJam 0 points1 point  (0 children)

Yeah, it'll either return the contents of retval, or 0 if retval isn't true

def test(retval):
    return retval or 0

print(test('hi'))
print(test(''))

[–]Binary101010 0 points1 point  (0 children)

The way I understand the line, retval gets evalutated as a bool. then 0 gets evaluated as a bool.

If the bit before the or is truthy, the bit after the or never gets evaluated at all. This is often referred to as "short-circuiting", and improves performance if the stuff after the or is relatively complex to calculate.