62

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?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
David Cournapeau
  • 78,318
  • 8
  • 63
  • 70
  • 12
    There's no scope `__main__`. There's an `if` that happens to compare a variable that happens to be called `__name__` to something that happens to be the string literal `"__main__"`. –  Jan 23 '11 at 18:53
  • @delnan: `__main__` is a module, and each module has an associated scope. Try `import __main__; type(__main__)` in the interpreter (not in IPython). – Sven Marnach Jan 23 '11 at 21:48
  • @Sven: I know. OP should know at some point. But it seemed to me OP instead confuses if statements with parts of the condition and think `if` introduces a new scope - which would be a more pressing issue. (Admittedly, "There is no scope `__main__` is misleading, strictly speaking) –  Jan 23 '11 at 21:50
  • 1
    @delnan: you're right. In retrospect, I don't know why I was confused. Maybe debugging code at 3 a.m is not such a good idea after all :) – David Cournapeau Jan 24 '11 at 01:30
  • 1
    I just stumbled over this exact same behaviour...thought I was going mad. Glad to see someone else has already asked this question! The behaviour is obvious when the answer is explained... – ccbunney Jun 28 '13 at 08:58
  • I was today years old when I discovered this thing. At least I'm not mad... – Avio Oct 19 '22 at 13:44

5 Answers5

44

Variables in the current modules global scope are visible everywhere in the module -- this rule also holds for the __main__ module.

From Guido's tutorial:

At any time during execution, there are at least three nested scopes whose namespaces are directly accessible:

  • the innermost scope, which is searched first, contains the local names
  • the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
  • the next-to-last scope contains the current module’s global names
  • the outermost scope (searched last) is the namespace containing built-in names
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 2
    @AA: The main script is treated by Python as a module with the name `__main__`. You even can do `import __main__` (but usually that's a Bad Idea). – Sven Marnach Jan 23 '11 at 19:16
  • Is it considered good practice to use variables defined in the `__main__` module in functions called in the `__main__` without explicitly passing them? – emilaz Apr 20 '20 at 09:23
  • 1
    @emilaz No, definitely not. Avoid global variables whenever you can. :) – Sven Marnach Apr 20 '20 at 09:29
16

The thing here is that:

if __name__ == "__main__":
    foo = "bar"

defines a global variable named foo in that script. so any function of that module will have access to it.

The piece of code listed above is global to the module and not inside any function.

Santiago Alessandri
  • 6,630
  • 30
  • 46
8

foo is a module global variable (it's not in any function). All scopes within the module can access it.

sinelaw
  • 16,205
  • 3
  • 49
  • 80
4

In python there's the global scope, and functions have their own scopes. So it you define foo under the name==main, it's in the global scope. Also, it's not a mistake to use a variable which hasn't been declared yet, in a function, if it will be declared by the time the function will be called.

kynnysmatto
  • 3,665
  • 23
  • 29
0

As sinelaw pointed out, the way out this annoyance and inadvertent bug/s is to use a function. This function can be within the 'if main:' like this:

if __name__ == "__main__":
    def mainlet():
        foo = "bar"
    mainlet()
giwyni
  • 2,928
  • 1
  • 19
  • 11