The Counter-Exception following the chapter's exercises can be a nasty surprise. Compare the two functions below and what Python does when the program runs.


Default Global

def FunctionOne(x):
  T = 2*x
  return None
def FunctionTwo(x):
  T[0] = 2*x
  return None
T = list(range(4))
FunctionOne(10)
print T
FunctionTwo(10)
print T


By a quick reading of the chapter, one might have the impression that neither of these functions should use T as a default global variable, because T is used in an assignment in each. However, upon scrutiny, we see that FunctionTwo does not assign to T: instead, FunctionTwo uses item assignment to T[0]. This is a crucial difference, so Python treats T as default global in FunctionTwo.

There's no easy way to explain why Python behaves this way. Later chapters explain how Python implements lists and dictionaries by a class/object philosophy, so that item assignments are actually indirect changes to variables.