zoukankan      html  css  js  c++  java
  • 作用域

    作用域

    注意:函数,模块,类都有自己的作用域

    for循环,while循环,if,else 都没有自己的作用域

    def test1():
    print("in the test1")
    def test():
    print("in the test")
    return test1=====》test1 是函数test1的IP地址
    s = test()========>运作test()结果是 in the test ,然后返回test1的IP,即目前在test1位置
    print(s)=====》打印test1 的IP
    所以结果是:

    in the test
    <function test1 at 0x7ff1e05d01e0>

    ******************************************************************

    def test1():
    print("in the test1")===>打印in the test1
    def test():
    print("in the test")====》打印in the test
    return test1()=====>返回函数test1()
    s = test()
    print(s)===》函数test1()没有返回值,所以默认None
    结果是:

    in the test
    in the test1
    None

    ********************************************************************

    def foo():
    name = "lihaifeng" 第1步
    def bar():
    name = "wupeiqi"
    print(name)
    return bar 第2步,返回函数bar的位置
    foo()======>运行函数foo(),首先进行第一步然后进行第二步,中间没用运行,所以不会输出结果
    print(foo())=====》打印函数bar的位置,所以结果是函数bar的IP
    foo()()====>foo()即bar 所以相当于调用函数bar(),结果是wupeiqi.

    注意:函数外部不执行里边肯定不执行。里边执行,外边肯定执行

     for循环没有作用域,赋值按照最后一个值例如:

    i = 0
    for i in range(5):
    i += 1
    print(i)#---结果是5
  • 相关阅读:
    linux uniq 命令实用手册
    linux sort 命令实用手册
    linux awk 命令实用手册
    如何高效使用vim
    15个有趣好玩的linux shell 命令
    一篇文章带你编写10种语言HelloWorld
    如何用hugo 搭建博客
    c++中的exit()
    枚举数据类型C++
    常见的字符测试函数
  • 原文地址:https://www.cnblogs.com/wode110/p/14493737.html
Copyright © 2011-2022 走看看