-1

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.

Tom Green
  • 1
  • 1

1 Answers1

1

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
Paul Hankin
  • 50,269
  • 11
  • 86
  • 107
  • Thanks, you're right, I was just learning python back then and knew next to nothing about its features! – Tom Green May 23 '22 at 14:38