I know why we need to declare nonlocal in Python and am a bit confused about the following example. Without nonlocal records in line 276, records in line 277 is not defined. However, records in line 289 can be used without any error.

And the following is the situation with nonlocal and it works well.

A
nonlocaldeclaration is analogous to aglobaldeclaration. Both are needed only when a function assigns to a variable. Normally, such a variable would be made local to the function. Thenonlocalandglobaldeclarations cause it to refer to the variable that exists outside of the function.If a function does not assign to a variable, then the declarations are not needed, and it automatically looks for it in a higher scope.