in this examplenotice how the function helloworld(uses the value of that' defined in the same environment as where helloworld(was defined thuseven though there is also an defined in foo py and that' where helloworld(is actually being calledthat value of is not the one that' used when helloworld(executes when the statements that make up function are packaged together with the environment in which they executethe resulting object is known as closure the behavior of the previous example is explained by the fact that all functions have _globals_ attribute that points to the global namespace in which the function was defined this always corresponds to the enclosing module in which function was defined for the previous exampleyou get the followinghelloworld _globals_ {' _builtins_ ''helloworld'' ' ' _name_ '' _main_ '' _doc_ 'none 'foo'when nested functions are usedclosures capture the entire environment needed for the inner function to execute here is an exampleimport foo def bar() def helloworld()return "hello world is %dx foo callf(helloworldreturns 'hello worldx is closures and nested functions are especially useful if you want to write code based on the concept of lazy or delayed evaluation here is another examplefrom urllib import urlopen from urllib request import urlopen (python def page(url)def get()return urlopen(urlread(return get in this examplethe page(function doesn' actually carry out any interesting computation insteadit merely creates and returns function get(that will fetch the contents of web page when it is called thusthe computation carried out in get(is actually delayed until some later point in program when get(is evaluated for examplepython page(jython page(python jython pydata python(fetches jydata jython(fetches in this examplethe two variables python and jython are actually two different versions of the get(function even though the page(function that created these values is no longer executingboth get(functions implicitly carry the values of the outer variables that were defined when the get(function was created thuswhen get( lib fl ff