0

Question about mecanism of a check conditions. if ONE or TWO: Will TWO condition checked, If ONE == True? Where can I read about this?

George Sh.
  • 240
  • 1
  • 8

3 Answers3

2

In python this is called short-circuiting. Logical expressions are evaluated left to right (taking into account brackets) and execution stops as soon as it is clear what the logical answer will be.

Try this code in the interactive console:

>>> def one():
...     print "one called"
...     return True

>>> def two():
...     print "two called"
...     return True

>>> one() or two()

The response will be:

one called
True

The same thing happens with and (if the first argument is False, the 2nd argument is never evaluated).

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
2

This is called short-circuiting, and Python does support it. You can read an explanation in the docs.

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
1

Yes, Python short-circuits the evaluation of Boolean expressions.

glglgl
  • 89,107
  • 13
  • 149
  • 217