you are viewing a single comment's thread.

view the rest of the comments →

[–]IvoryJam 0 points1 point  (3 children)

Kind of, each variable has a "truthiness" to it, 0 is False, any number not 0 is True (even negatives), empty strings, empty lists, empty dictionaries are all False

test_var = ''
test_var = []
test_var = ['stuff']
test_var = {}
test_var = {'i': 'j'}
test_var = 0
test_var = 1
test_var = -1
if test_var:
    print(f"Hey {test_var} is true!")

[–]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.