python __main__ 里的变量是全局变量 因此在函数里面可以访问到
__main__ and scoping in python
from:https://stackoverflow.com/questions/4775579/main-and-scoping-in-python
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?
- 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
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
- @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
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.
foo is a module global variable (it's not in any function). All scopes within the module can access it.
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.
python __main__ 里的变量是全局变量 因此在函数里面可以访问到的更多相关文章
- python世界里的局部变量和全局变量: 潜规则太重要了!!!
python世界里的局部变量和全局变量: 潜规则太重要了!!! 先上代码: def fun(): def test_global(): ''' 内层和外层都需要声明为global, 才能彻底打通变量名 ...
- python学习-day15:局部变量与全局变量、嵌套函数、递归
一.全局变量与局部变量 在子程序中定义的变量称为局部变量, 在程序的一开始定义的变量称为全局变量. 全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序.当全局变量与局部变量同名时:在定义局部 ...
- python正则表达式里引入变量
import re def reg_exp(senten): jiqiren = "阿童木" matchObj1 = re.search( r'(你(.*?)(男|女))|(机器( ...
- python做中学(一)全局变量的用法
一段时间没有使用python来写代码,就发现以前学习的很多语法都忘了.看来还是当初这方面的项目做的好不够多,没有系统性的运用和学习,导致了很多语法不能顺手拈来.在接下来的这个项目中, 一定要把遇到的一 ...
- 如何查看 Python 全部内置变量和内置函数?
https://jingyan.baidu.com/article/7082dc1c071649e40a89bdb8.html Python 解释器内置了一些常量和函数,叫做内置常量(Built-in ...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- 说几个python与c区别的地方以及静态变量,全局变量的区别
一: python代码: a = 2 def b(): print a a = 4 print a b() 在b函数中,有a=4这样的代码,说明a是函数b内部的局部变量,而不是外部的那个值为2的全局变 ...
- python 本地变量和全局变量 locals() globals() global nonlocal 闭包 以及和 scala 闭包的区别
最近看 scala ,看到了它的作用域,特此回顾一下python的变量作用域问题. A = 10 B = 100 print A #10 print globals() #{'A': 10, 'B': ...
- python开发_python中的变量:全局变量和局部变量
如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...
随机推荐
- [LeetCode] 361. Bomb Enemy 炸敌人
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- [LeetCode] 482. License Key Formatting 注册码格式化
You are given a license key represented as a string S which consists only alphanumeric character and ...
- 使用Centos7.5+Nginx+Gunicorn+Django+Python3部署blog项目
项目开发环境是 Python3.5.2+Django1.10.6+Sqlite3+Centos7.5+Nginx1.12.2+Gunicorn 发布出来供需要的同学借鉴参考.文中如有错误请多多指正! ...
- LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...
- 题解 P3957 【跳房子】
对于这题有一个不用单调队列并且不是玄学设置区间最大值的做法 这题校内模拟考的时候打二分+枚举,结果写炸了,跑过来看题解发现为什么他们的区间最大值都是 $ 1005 $ ???特别懵,其实我的代码在dp ...
- 长乐培训Day4
T1 矩阵 题目 [题目描述] 从前有个 n×m 的矩阵,初始时每个位置均为 0.你需要依次执行 q 个操作,每个操作会指定一行或一列,然后将该行或该列的所有元素全部赋为一个相同的值. 输出操作完成后 ...
- python 之 Django框架(模板系统、过滤器、simple_tag、inclusion_tag、Tags、母版、组件)
12.35 Django模板系统 {{ }}和 {% %},变量相关的用{{}},逻辑相关的用{%%} app02/views: # 模板语言测试函数 def template_test(reques ...
- python 之 Django框架(服务器程序和应用程序、基础必备三件套及相关配置)
第十二章 Django框架 12.1 服务器程序和应用程序 服务器程序负责对socket服务器进行封装,并在请求到来时,对请求的各种数据进行整理.应用程序则负责具体的逻辑处理.为了方便应用程序的开发, ...
- docker (二):容器container
docker使用入门(二):容器container docker层次结构可以分为三层,从下往上是:容器(container).服务(services).堆栈(stack),其中services定义了容 ...
- python爬取b站排行榜视频信息
和上一篇相比,差别不是很大 import xlrd#读取excel import xlwt#写入excel import requests import linecache import wordcl ...