• 零基础学python-16.2 作用域法则


    作用域法则:

    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	est.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-目录

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    用户故事与敏捷方法阅读笔记2
    用户故事与敏捷方法阅读笔记1
    梦断代码阅读笔记3
    梦断代码阅读笔记2
    梦断代码阅读笔记1
    疫情可视化项目-进度2
    疫情可视化项目-进度1
    每日总结3.16
    Android体温记录器更新-进度5
    每日总结3.11
  • 原文地址:https://www.cnblogs.com/raylee2007/p/4896740.html
Copyright © 2020-2023  润新知