How shall I resolve a nonlocal name in a language with deep binding? From Programming Language Pragmatics by Scott, 




  Figure 3.14 Deep binding in Python. At right is a conceptual view of the run-time stack.
  Referencing environments captured in closures are shown as dashed boxes and arrows. When
  B is called via formal parameter P, two instances of I exist. Because the closure for P was
  created in the initial invocation of A, B’s static link (solid arrow) points to the frame of that earlier
  invocation. B uses that invocation’s instance of I in its print statement, and the output is a 1.
  
  The catch is that a running program may have more than one instance of an object that is declared within a recursive subroutine. A closure in a language with
  static scoping captures the current instance of every object, at the time the closure
  is created. When the closure’s subroutine is called, it will ﬁnd these captured instances, even if newer instances have subsequently been created by recursive calls.


So basically, the quote tries to explain that the following program (which is the same as the one in the screenshot) prints out 1:

def A(I, P):
    def B():
        print(I)
    # body of A:
    if I &gt; 1:
        P()
    else:
        A(2, B)

def C():
    pass # do nothing

A(1, C) # main program


I don't quite understand the reason being "because the closure for P was
created in the initial invocation of A, B’s static link (solid arrow) points to the frame of that earlier invocation", and "When the closure’s subroutine is called,  it will ﬁnd these captured instances". So I modify the example, and then the new example prints out 2 instead of 1:

def A(I, P):
    def B():
        print(I)
    if I &gt; 2:
        P()
    elif I &gt; 1:
        A(3, B)
    else:
        A(2, B)

def C():
    pass

A(1, C)


Another modified example prints 1:

def A(I, P):
    def B():
        print(I)
    if I &gt; 2:
        P()
    elif I &gt; 1:
        A(3, P)
    else:
        A(2, B)

def C():
    pass

A(1, C)


So how can I know which closure matters when resolving a nonlocal name in a language with deep binding? 

Generally, is a closure created whenever a function is passed as an argument to another function?

Thanks.
Roughly put, every time you define a function, as in def B(): ... you define B to be a closure. This is a pair contains a representation of the body of the function (possibly compiled to a simpler language), and a reference to the current environment (possibly restricted to what B actually needs). The current environment is the one we are in when we run def B(): ....

In the example you provide, B is defined inside A, so its associated closure gets a reference the the current call frame of A. This is why, if you pass P=B in the recursive call, after the call running P prints the value of I in the previous call frame.

Note that, in many languages, closures can be returned, which makes the current call frame to be not freed after the return. For example,

def A(I):
  def B():
    print(I)
  def C():
    nonlocal I  # Python for "use A's I, don't create a fresh local I"
    I += 1
  return (B,C)

# main
(b,c) = A(10)
b() # 10
c()
b() # 11
c()
b() # 12


Above, we can see that, even if A returned, its local variable I is still live! It can be accessed through the two returned closures, which contain a reference to the A's call frame, even if A is now finished.