0

I just ran into this case that I had never noticed before. A few different cases:

>>> [] and True
[]
>>> True and []
[]
>>> [] and False
[]
>>> False and []
False

What's the implementation logic behind this? I would expect the AND operator to return a boolean

accdias
  • 5,160
  • 3
  • 19
  • 31
micric
  • 621
  • 4
  • 15
  • 4
    `[]` is falsy. `[] and True` is therefore falsy, returning the first falsy operand via short-circuiting. The rest follow by analogy. – erip Aug 26 '22 at 14:17
  • interestingly, the output is silenced when entering something like `True and None`. I assume that's because it returns `None`, correct? – rv.kvetch Aug 26 '22 at 14:20
  • 1
    @rv.kvetch AFAIK that's a detail of the REPL, not Python per se – Jared Smith Aug 26 '22 at 14:21
  • 2
    @rv.kvetch When using the REPL, only the outputs which are not None get automatically printed. – Thierry Lathuille Aug 26 '22 at 14:22
  • 1
    ahh, totally makes sense. I tried entering just None in the REPL, and got blank output, which also confirms it for me. – rv.kvetch Aug 26 '22 at 14:22
  • Why is it called short circuiting? Doesn't seem like an intuitive name for this behavior. – Jeff Gruenbaum Aug 26 '22 at 14:36
  • 1
    @JeffGruenbaum Because if you do something like a() or b(), b will not be evaluated if a is true; the rest of the statement is short circuited. It is important behaviour in C, which arguably is a parent language of Python. – Max Aug 26 '22 at 14:57

0 Answers0