recursive functionswhich may be preferable in some contexts the following is one such transitive reloaderit uses generator expression to filter out nonmodules and modules already visited in the current module' namespace because it both pops and adds items at the end of its listit is stack basedthough the order of both pushes and dictionary values influences the order in which it reaches and reloads modules--it visits submodules in namespace dictionaries from right to leftunlike the left-to-right order of the recursive versions (trace through the code to see howwe could change thisbut dictionary order is arbitrary anyhow ""reloadall pytransitively reload nested modules (explicit stack""import types from imp import reload from reloadall import statustryreloadtester from required in def transitive_reload(modulesvisited)while modulesnext modules pop(delete next item at end status(nextreload thispush attrs tryreload(nextvisited add(nextmodules extend( for in next __dict__ values(if type( =types moduletype and not in visiteddef reload_all(*modules)transitive_reload(list(modules)set()if __name__ ='__main__'tester(reload_all'reloadall 'test codereload myselfif the recursion and nonrecursion used in this example is confusingsee the discussion of recursive functions in for background on the subject testing reload variants to prove that these work the samelet' test all three of our reloader variants thanks to their common testing functionwe can run all three from command line both with no arguments to test the module reloading itselfand with the name of module to be reloaded listed on the command line (in sys argv) :\codereloadall py reloading reloadall reloading types :\codereloadall py reloading reloadall reloading types :\codereloadall py advanced module topics