'spamdef func()global 'nifunc(print( what about this code--what' the outputand whyx 'spamdef func() 'nidef nested()print(xnested(func( how about this examplewhat is its output in python xand whydef func() 'nidef nested()nonlocal 'spamnested(print(xfunc( name three or more ways to retain state information in python function test your knowledgeanswers the output here is 'spam'because the function references global variable in the enclosing module (because it is not assigned in the functionit is considered global the output here is 'spamagain because assigning the variable inside the function makes it local and effectively hides the global of the same name the print statement finds the variable unchanged in the global (modulescope it prints 'nion one line and 'spamon anotherbecause the reference to the variable within the function finds the assigned local and the reference in the print statement finds the global this time it just prints 'nibecause the global declaration forces the variable assigned inside the function to refer to the variable in the enclosing global scope the output in this case is again 'nion one line and 'spamon anotherbecause the print statement in the nested function finds the name in the enclosing function' local scopeand the print at the end finds the variable in the global scope scopes