Search HoldMyTwist

Browse all posts

Episode 30

The Bell That Never Rang

Duck wants to make sure the 2 parts of a 2-check expression execute, such as a check that rings a bell. In Python you can use the 1st answer to make an AND or OR outcome. This may mean that the 2nd check is never executed.

Episodes

Computing Basics

    Duck has a checklist toy for a couple of check stations. Robot looks at an untouched bell, the call counter of which is zero.
    Duck points at the unpushed bell.
    Duck raises the striker for bell. Robot keeps it steady next to the arrow from ticket check to bell check.
    Robot shows False, and duck's ring_check(). Duck stands next to the bell waiting for the result.
    Robot goes directly from the false result to the combined result and skips the bell check. Duck keeps the striker down and the bell counter is zero.
    The bell didn't go. Duck checks his counter. It's still zero calls.
    Duck is removing a supposed result from this check because it wasn't called.
    Duck lowers approval stamp (negative on the whole) next to False. The toy list stays closed since expression failed.
    Duck changes ticket result to true (in another run) and resets bell counter to 0. Robot points to same and operator.
    The ticket function gives True, so we start a new run. Then we call the bell check once. It rings once and gives True. The net result is True.
    Duck is comparing 2 runs. A false first result does not ring the bell. A true first result does ring the bell. The bell function itself has not changed.
    Robot points out boolean return types on this example. Duck leaves the finished run counter set to 1.
    Duck shows us True or ring_check() on a new run with bell counter reset to 0.
    Robot sends true left result right to or result. Duck lets striker sit and counter remains 0.
    The first one for another execution gives False and the operator is or. The bell check executes only once and gives True. Counter is incremented by one call.
    Duck puts striker to the side next to four different run logs. Robot checks the number of calls: and false zero, true one or true zero, false one.

    Episode transcript

    A silent bell check

    Duck has a checklist toy for a couple of check stations. Robot looks at an untouched bell, the call counter of which is zero.

    Diagram values: has_ticket() and ring_check()

    Duck: I asked 2 checks but one replied.

    Duck points at the unpushed bell.

    Diagram values: has_ticket(): False; ring_check(): ring; True

    Robot: Ticket check says false. When I call bell check it rings once and says true.

    Duck raises the striker for bell. Robot keeps it steady next to the arrow from ticket check to bell check.

    Duck: Surely both of these get executed, right?

    Robot shows False, and duck's ring_check(). Duck stands next to the bell waiting for the result.

    Diagram values: False and ring_check()

    Robot: If I get false ticket result, and use and operator, will the bell function be called?

    Watch the second check

    Robot goes directly from the false result to the combined result and skips the bell check. Duck keeps the striker down and the bell counter is zero.

    Diagram values: False → False; BELL ROUTE SKIPPED; RESULT False; BELL CALLS 0; SECOND SKIPPED

    Robot: AND requires both. So when first answer is false, the result is false already, so bell check is skipped in Python.

    The bell didn't go. Duck checks his counter. It's still zero calls.

    Diagram values: BELL CALLS 0

    Duck: the bell check was never called.

    Duck is removing a supposed result from this check because it wasn't called.

    Diagram values: RIGHT NOT EVALUATED

    Robot: There is no return value from that uncalled bell function.

    Duck lowers approval stamp (negative on the whole) next to False. The toy list stays closed since expression failed.

    Diagram values: RESULT False

    Duck: So I guess I can't say that all the checks were successful.

    Change the first answer

    Duck changes ticket result to true (in another run) and resets bell counter to 0. Robot points to same and operator.

    Diagram values: True and ring_check()

    Duck: Now the ticket checking returns true. So does the bell get a chance?

    The ticket function gives True, so we start a new run. Then we call the bell check once. It rings once and gives True. The net result is True.

    Diagram values: BELL CALLS 1; RESULT True

    Robot: Yes.

    Duck is comparing 2 runs. A false first result does not ring the bell. A true first result does ring the bell. The bell function itself has not changed.

    Diagram values: False: SKIP; True: CALL

    Duck: Same bell check. But 1st answer was different.

    Robot points out boolean return types on this example. Duck leaves the finished run counter set to 1.

    Diagram values: BOOL RETURNS

    Robot: These functions will return Booleans. But Python can return other operand values.

    Try an either rule

    Duck shows us True or ring_check() on a new run with bell counter reset to 0.

    Diagram values: True or ring_check()

    Duck: Ticket is true. How many bell calls?

    Robot sends true left result right to or result. Duck lets striker sit and counter remains 0.

    Diagram values: RESULT True; BELL CALLS 0

    Robot: So Python immediately returns that value. No ring_check is called.

    The first one for another execution gives False and the operator is or. The bell check executes only once and gives True. Counter is incremented by one call.

    Diagram values: False or ring_check(); BELL CALLS 1

    Duck: So Python only executes ring_check once. It rings the bell once and returns True.

    Duck puts striker to the side next to four different run logs. Robot checks the number of calls: and false zero, true one or true zero, false one.

    Diagram values: OPERATOR | LEFT TEST | BELL CALLS; AND | False | 0; AND | True | 1; OR | True | 0; OR | False | 1

    Robot: Python evaluates left-hand operand first.

    The lesson

    Python evaluates left-hand operand first. For and, if left-hand operand is false then it is returned immediately. Else right-hand operand is evaluated and returned. For or, if left-hand operand is true then it is returned immediately. Else right-hand operand is evaluated and returned. Our toy functions return Boolean values.

    Try it

    Diagram values: first() and (second() or ring_check()); first() = True; second() = True; INITIAL CALL COUNTS 0

    How many times are the functions called and what is the result of the expression?

    Answer

    Python calls first(), gets true, and evaluates the parenthesized expression. It calls second() and gets true. The or short-circuits and skips the call to ring_check(). The whole thing ends up with True. First and second get called once. bell gets called zero times.

    Sources

    Python language reference: Boolean operations (opens in a new tab)