__main__ and scoping in python from:https://stackoverflow.com/questions/4775579/main-and-scoping-in-python Ask Question 28 3 I was somehow surprised by the following behavior: def main(): print "%s" % foo if __name__ == "__main__": foo = "bar" main() i.e. a module function has access to enclosing variables in the __main__. What's the explanation for it? python scope share improve this question edited Jan 23 '11 at 18:41 SilentGhost 206k4949 gold badges271271 silver badges268268 bronze badges asked Jan 23 '11 at 18:30 David Cournapeau