closures are nothing to fear they can actually be very useful and powerful tool in the right circumstancein nutshella closure is any dynamically-generated function returned by another function the key property is that the returned function has access to the variables in the local namespace where it was created here is very simple exampledef make_closure( )def closure()print(' know the secret%dareturn closure closure make_closure( the difference between closure and regular python function is that the closure continues to have access to the namespace (the functionwhere it was createdeven though that function is done executing so in the above casethe returned closure will always print know the secret whenever you call it while it' common to create closures whose internal state (in this exampleonly the value of ais staticyou can just as easily have mutable object like dictsetor list that can be modified for examplehere' function that returns function that keeps track of arguments it has been called withdef make_watcher()have_seen {def has_been_seen( )if in have_seenreturn true elsehave_seen[xtrue return false return has_been_seen using this on sequence of integers obtainin [ ]watcher make_watcher(in [ ]vals [ in [ ][watcher(xfor in valsout[ ][falsefalsefalsetruetruetruefalsetruehoweverone technical limitation to keep in mind is that while you can mutate any internal state objects (like adding key-value pairs to dict)you cannot bind variables in the enclosing function scope one way to work around this is to modify dict or list rather than binding variablesdef make_counter()count [ def counter()functions www it-ebooks info