Personal blog and documentation

@aRkadeFR blog and documentation on system and development

A good reminder before starting the article is to read the document of the global statement, and the nonlocal statement.

Plus read the part of the language reference on the naming and binding, and then how the attributes and methods are called.

We know that we should use the variables in the smaller scope possible, but what are the globals and nonlocal variables for then?

Let’s see the global interface quickly :

"""
tests for the global variables
"""
var1 = 1


def fct1():
    global var1
    var1 = 1
    print("this is my global value: {}".format(var1))


def fct2():
    var1 = 2
    print("this is not my global value: {}".format(var1))


if __name__ == "__main__":
    print("""------------------------------
test of the global variables
------------------------------""")
    print("this is my global value: {}".format(var1))
    fct1()
    fct2()
    print("this is my global value: {}".format(var1))

output >
------------------------------
test of the global variables
------------------------------
this is my global value: 1
this is my global value: 1
this is not my global value: 2
this is my global value: 1

As you see, it’s neat. Python3 with CPython (and all others if they respect the standard), access the value of the name variable which is in the closer scope.

If you want to access the global variable, you have to specifically tell python3 with the global statement.

I’m not going into the debat about when and where to use the globals. I will only advise to you that variable should be in the closer scope.

Now what about the nonlocal variable?

And now it’s getting very interesting…

We gonna see all the power of the nonlocal statement with some closure.

The very basic example is :

def start(x):
	def summarize(y):
		nonlocal x
		return x+y
	return summarize

As you can see, we specifically states the x variables is nonlocal and come from the outer function (start).

This is a good way to help the readability of your programs.