TL;DR: I want a locals() that looks in a containing scope.

Hi, all.

I'm teaching a course on Python programming to some chemist friends, and I want to be sure I really understand scope. 

Consider:

```
def a():
    x = 1
    def b():
        print(locals())
        print(globals())
    b()

```

Locals prints an empty environment, and globals prints the usual globals. How do I get access to the environment where x is stored? Clearly the interpreter knows about it because I can refer to it.

Related: When does scoping happen? The following nameErrors on a = x+2 only if x=3 is included:

```
def a():
    x = 1
    def b():
        a = x+2
        x = 3
    b()

```

If you comment out x=3, the code works. Does this mean that python makes a lexical-scope pass over the code before it interprets it?