
正文
python __main__ 里的变量是全局变量 因此在函数里面可以访问到
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
__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
shareimprove 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
59.5k77 gold badges5757 silver badges6767 bronze badges
-
4There's no scope
__main__. There's anifthat happens to compare a variable that happens to be called__name__to something that happens to be the string literal"__main__". – user395760 Jan 23 '11 at 18:53 -
@delnan:
__main__is a module, and each module has an associated scope. Tryimport __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
ifintroduces a new scope - which would be a more pressing issue. (Admittedly, "There is no scope__main__is misleading, strictly speaking) – user395760 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 1:30
-
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 8:58
add a comment
4 Answers
activeoldestvotes
activeoldestvotes
25
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
shareimprove this answer
edited Jan 23 '11 at 18:45
answered Jan 23 '11 at 18:32
Sven Marnach
375k8484 gold badges766766 silver badges710710 bronze badges
-
__main__module ? – user225312 Jan 23 '11 at 18:58 -
@AA: The main script is treated by Python as a module with the name
__main__. You even can doimport __main__(but usually that's a Bad Idea). – Sven Marnach Jan 23 '11 at 19:16
add a comment
7
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.
shareimprove this answer
answered Jan 23 '11 at 18:34
Santiago Alessandri
5,0682424 silver badges4242 bronze badges
add a comment
6
foo is a module global variable (it's not in any function). All scopes within the module can access it.
shareimprove this answer
answered Jan 23 '11 at 18:32
sinelaw
12.9k11 gold badge3636 silver badges7373 bronze badges
add a comment
2
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.
shareimprove this answer
edited Jan 23 '11 at 18:39
answered Jan 23 '11 at 18:34
kynnysmatto
2,4931717 silver badges23






