in comprehension expression such as [ for in ibecause they might clash with other names and reflect internal state in generatorsin xsuch variables are local to the expression itself in all comprehension formsgeneratorlistsetand dictionary in xthey are local to generator expressions and set and dictionary compressionsbut not to list comprehensions that map their names to the scope outside the expression by contrastfor loop statements never localize their variables to the statement block in any python see for more details and examples exception variables--the variable used to reference the raised exception in try statement handler clause such as except as because they might defer garbage collection' memory recoveryin xsuch variables are local to that except blockand in fact are removed when the block is exited (even if you've used it earlier in your code!in xthese variables live on after the try statement see for additional information these contexts augment the legb rulerather than modifying it variables assigned in comprehensionfor exampleare simply bound to further nested and special-case scopeother names referenced within these expressions follow the usual legb lookup rules it' also worth noting that the class statement we'll meet in part vi creates new local scope too for the names assigned inside the top level of its block as for defnames assigned inside class don' clash with names elsewhereand follow the legb lookup rulewhere the class block is the "llevel like modules and importsthese names also morph into class object attributes after the class statements ends unlike functionsthoughclass names are not created per callclass object calls generate instanceswhich inherit names assigned in the class and record per-object state as attributes as we'll also learn in although the legb rule is used to resolve names used in both the top level of class itself as well as the top level of method functions nested within itclasses themselves are skipped by scope lookups--their names must be fetched as object attributes because python searches enclosing functions for referenced namesbut not enclosing classesthe legb rule still applies to oop code scope example let' step through larger example that demonstrates scope ideas suppose we wrote the following code in module fileglobal scope def func( )local scope scopes and func assigned in moduleglobal and assigned in functionlocals is global