-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
  • Please try to [isolate problems](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) before posting. For example, if `[1].append(3) or 2` doesn't give the result you expect, then **check first** whether `[1].append(3)` by itself is doing what you expect in that context. – Karl Knechtel Sep 14 '22 at 15:42

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
  • 54,811
  • 11
  • 92
  • 118
  • 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