dir()
without arguments depends on the current scope:
Without arguments, return the list of names in the current local scope.
Inside the generator expression the scope changes:
>>> list(dir() for _ in ("re", "os"))
[['.0', '_'], ['.0', '_']]
This is also true for list, set, and dict comprehensions:
aside from the iterable expression in the leftmost for
clause, the comprehension is executed in a separate implicitly nested scope. This ensures that names assigned to in the target list don't "leak" into the enclosing scope.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…