零基础学python-16.2 作用域法则
2015-09-12 23:23
579 查看
作用域法则:
1.模块中定义的顶级变量,作用域是整个模块
我们把上面的代码保存为test.py
在上面的代码里面,我们定义了一个在模块里面通用的变量x,他是整个模块都可以使用的,就像test2引用x
然而,我们把上面的代码修改成下面的样子:
输出:
异常提示找不到y,因为y的作用域只在test1里面,离开了test1,y就不存在了
我们再引用两个例子来说明:
模块test1.py
模块test2.py
我们第一次先运行test1.py,然后打印x和id(x)出来
这个时候在idle里面可以引用x
同时我们运行test2.py
这个时候x出现异常,然后x跟着就指向不同的对象
从上面的实验可以看出,变量x在不同模块之间是不能够互相调用,作用域只存在与模块里面
2.每次对函数的调用都创建一个新的本地作用域
我们把下面的代码保存为test.py
然后在idle里面运行
两次输出的结果id都不一样,也就是引用变了,内存地址变了,其实调用的时候是引用一个新的命名空间
3.赋值的变量除非是命名为全局变量或非本地变了,否则均为本地变量
上面的代码,x是全局变量,y是非本地变量,其他的z、a、b都是本地变量
运行完之后我们再在idle里面输出x,y,z,a,b,c只有x存在,其他都不存在
4.所有变量名都可以归纳为本地、内置或者全局的
总结:我们这一章节简单说明了作用域的法则
这一章节就说到这里,谢谢大家
------------------------------------------------------------------
点击跳转零基础学python-目录
1.模块中定义的顶级变量,作用域是整个模块
def test(): print(id(x)) print(x) def test2(): print(x) if __name__=='__main__': x=100000 test() test2()
我们把上面的代码保存为test.py
在上面的代码里面,我们定义了一个在模块里面通用的变量x,他是整个模块都可以使用的,就像test2引用x
然而,我们把上面的代码修改成下面的样子:
def test(): print(id(x)) print(x) y=1 print(y) def test2(): print(x) if __name__=='__main__': x=100000 test() test2() print(y)
输出:
>>> ================================ RESTART ================================ >>> 20144208 100000 1 100000 Traceback (most recent call last): File "C:\Python34\test.py", line 12, in <module> print(y) NameError: name 'y' is not defined >>>
异常提示找不到y,因为y的作用域只在test1里面,离开了test1,y就不存在了
我们再引用两个例子来说明:
模块test1.py
def test(): print(id(x)) print(x) y=1 print(y) def test2(): print(x) if __name__=='__main__': x=100000 test() test2()
模块test2.py
def test(): pass def test2(): pass if __name__=='__main__': try: print(x) except:print('x is not exit') x='abc' print(id(x))
我们第一次先运行test1.py,然后打印x和id(x)出来
>>> ================================ RESTART ================================ >>> 20144208 100000 1 100000 >>> x 100000 >>> id(x) 20144208
这个时候在idle里面可以引用x
同时我们运行test2.py
>>> ================================ RESTART ================================ >>> x is not exit 12231040 >>> x 'abc' >>> id(x) 12231040 >>>
这个时候x出现异常,然后x跟着就指向不同的对象
从上面的实验可以看出,变量x在不同模块之间是不能够互相调用,作用域只存在与模块里面
2.每次对函数的调用都创建一个新的本地作用域
我们把下面的代码保存为test.py
def test(): print(id(x)) print(x) if __name__=='__main__': x='e f t efdad' test()
然后在idle里面运行
>>> ================================ RESTART ================================ >>> 31843176 e f t efdad >>> ================================ RESTART ================================ >>> 31843136 e f t efdad >>>
两次输出的结果id都不一样,也就是引用变了,内存地址变了,其实调用的时候是引用一个新的命名空间
3.赋值的变量除非是命名为全局变量或非本地变了,否则均为本地变量
def test(): global x y=3 def test2(): print(y) x=2 z=1 a='a' b='b' if __name__=='__main__': x='e f t efdad' test()
上面的代码,x是全局变量,y是非本地变量,其他的z、a、b都是本地变量
>>> x 2 >>> y Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> y NameError: name 'y' is not defined >>> a Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> a NameError: name 'a' is not defined b >>> c Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> c NameError: name 'c' is not defined >>> z Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> z NameError: name 'z' is not defined >>>
运行完之后我们再在idle里面输出x,y,z,a,b,c只有x存在,其他都不存在
4.所有变量名都可以归纳为本地、内置或者全局的
总结:我们这一章节简单说明了作用域的法则
这一章节就说到这里,谢谢大家
------------------------------------------------------------------
点击跳转零基础学python-目录
相关文章推荐
- Python PIL图片数据操作
- win10 python 二维码生成
- python中的 @ 修饰符
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"
- 《head first python》——定制数据对象
- [有码有真相]python unittest2 源码解读及各类运行测试方法示例
- python简单爬虫例子(一)
- python对字符串实现四则运算
- Python 爬虫 urllib2异常处理
- Python学习----错误、调试和测试
- numpy函数fromfunction分析
- [python] 爬取乌云知识库的标题
- Python+OpenCV学习(8)---Harris_corner
- python简单小记
- python模拟新浪微博登陆之获取cookies
- Python如何调用R:rpy2使用介绍
- python-property
- python-property
- python-traceback
- python学习——collections模块