This example is a nice way to see how binding works, using the stepper. The function being defined tests whether the letter "a" occurs earlier than the letter "b" in a string.


Example of Binding

def normal(Word):
  aplace = Word.lower().index("a")
  bplace = Word.lower().index("b")
  a_before_b = aplace < bplace
  return a_before_b
X = normal("Amoeba")
Y = normal("Black")
Z = normal("ably")


One thing that initially seems strange in stepping forward though the example is how Python skips over the function body, jumping to the line X = normal("Amoeba"). The reason for this is that Python stores the function definition for later, when it will be needed. Then, when X, Y, and Z are determined, Python goes back to the body of normal, doing the work there, and then jumping back once the evaluation of normal is done.

Running the example shows that the parameter "Word" is bound three times as the program runs, once for each argument. In turn, the names for expressions within the function body, during the time that a result is being calculated, are obtained, with the help of the index string method introduced near the end of the chapter on operators.