Why does the 'logical short-circuit' principle in python fail?

30 views Asked by At

Compared to the code below, does python's "logical short-circuit" rule fail? If so, why is it not working?'

print([1].append(3) or 2)

The result is '2',the 'logical short circuit' principle seems to have failed

print([1,3] or 2)

the result is '[1,3]',the'logical short circuit' principle is valid.

1

There are 1 answers

1
Paul Hankin On

Calls to append, like [1].append(3), return None (they update the list, but that's not visible in this snippet of code). print([1].append(3) or 2) is like print(None or 2) which is like print(2) because None is false-ish.

For example:

>>> print([1].append(3))
None