python __main__ 里的变量是全局变量 因此在函数里面可以访问到

28

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?

  • 4
    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__". – user395760 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) – 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

4 Answers

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
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.

6

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

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.

posted @   bonelee  阅读(5770)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2018-07-16 Esper——内存计算、事件驱动、SQL支持
点击右上角即可分享
微信分享提示