on related noteit' also possible to change mutable object in the enclosing scope in and without declaring its name nonlocal the followingfor exampleworks the same as the previous versionis just as portableand provides changeable per-call statedef tester(start)def nested(label)print(labelstate[ ]state[ + state [startreturn nested leverage in-place mutable change extra syntaxdeep magicthis leverages the mutability of listsand like function attributesrelies on the fact that in-place object changes do not classify name as local this is perhaps more obscure than either function attributes or ' nonlocalthough-- technique that predates even function attributesand seems to lie today somewhere on the spectrum from clever hack to dark magicyou're probably better off using named function attributes than lists and numeric offsets this waythough this may show up in code you must use to summarizeglobalsnonlocalsclassesand function attributes all offer changeable state-retention options globals support only single-copy shared datanonlocals can be changed in onlyclasses require basic knowledge of oopand both classes and function attributes provide portable solutions that allow state to be accessed directly from outside the stateful callable object itself as usualthe best tool for your program depends upon your program' goals we'll revisit all the state options introduced here in in more realistic context--decoratorsa tool that by nature involves multilevel state retention state options have additional selection factors ( performance)which we'll have to leave unexplored here for space (we'll learn how to time code speed in for nowit' time to move on to explore argument passing modes why you will carecustomizing open for another example of closures at workconsider changing the built-in open call to custom versionas suggested in this earlier sidebar "breaking the universe in python xon page if the custom version needs to call the originalit must save it before changing itand retain it for later use-- classic state retention scenario moreoverif we wish to support multiple customizations to the same functionglobals won' dowe need per-customizer state the followingcoded for python in file makeopen pyis one way to achieve this (in xchange the built-in scope name and printsit uses nested scope closure to remember value for later usewithout relying on global variables--which can clash and allow just one valueand without using class--that may require more code than is warranted hereimport builtins why nonlocalstate retention options