zoukankan      html  css  js  c++  java
  • 全局变量 局部变量 递归

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    # 1.局部变量,全局变量,没有缩进的变量就是全局变量。有缩进的被成为局部变量,子程序里的变量为局部变量。全局变量尽量遵循用大写表示,局部
    # 变量我们尽量用小写来定义。例:
    # name ="张三" #这里定义的为全局变量
    # def ce():
    # name="测试" #这个就是局部变量。
    # print(name)
    # print(name)
    # ce()
    # 2.global在子程序里引用全局变量。
    # NAME="slow"
    # def sudu():
    # global NAME
    # NAME="ZHANSAN"
    # print(NAME)
    # print(NAME)
    # sudu()
    # print(NAME)
    # 错误的示例:
    # Name="zhang"
    # def test():
    # Name="ss"
    # global Name
    # print(Name)
    # 3.变量读取的时候优先读取局部变量,能读取全局变量,但无法对其赋值。但对于可变变量,可以对其进行内部操作,例列表,字典等可以使用它们自
    # 身的方法 例:
    # name=[2,3,4,5]
    # def test(x):
    # global name
    # name.append(x)#可以使用列表的内置方法
    # test("22")
    # print(name)

    # 4变量的几种存在方式,有声明局部变量的时候,无global优先读取局部变量,有global的时候读取全局变量。
    # 无声明局部变量,没有global
    # name="zhansan"
    # def test(x):
    # print(x)
    # print(name)
    # print(name)
    # test("zs")
    # 有声明 有global的
    # name="lisi"
    # def test():
    # global name
    # name="wangwu"
    # print(name)
    # print(name)
    # test()
    # print(name)
    # 无声明,无global的
    # ls=[3,4,5,6]
    # def xz():
    # ls.extend([77,88,99])
    # print(ls)
    # xz()
    # print(ls)
    # 5,嵌套和global的指向最外层的变量,nonlcal上层的指向,dubug断点测试程序的指向
    # name="第一层"
    # def er():
    # name="第二层"
    # print(name)
    # def san():
    # nonlocal name
    # name="第三层"
    # print(name)
    # def tsi():
    # name="第四层"
    # print(name)
    # tsi()
    # print("4")
    # san()
    # print(name)
    # print(name)
    # er()
    # 6.风湿理论,函数即变量
    # 错误示例:
    # def test1():
    # name=13
    # test2()
    # 在python内部中,每当遇到def就会划出一块内存空间,把def和逻辑代码块作为字符存储下来,这和变量的形式是一样的,当我们不调用它的时候,编译
    # 后,该函数即已存在,所以无论顺序的先后是不影响其调用的
    # def test1(x):
    # test2()
    # print(x)
    # def test2():
    # print("x2")
    # test1(13)
    # 最好的体现还是在此示例:
    # name="第一层"
    # def er():
    # name="第二层"
    # print(name)
    # def san():
    # nonlocal name
    # name="第三层"
    # print(name)
    # def tsi():
    # name="第四层"
    # print(name)
    # tsi()
    # print("4")
    # san()
    # print(name)
    # print(name)
    # er()
    # 第一层name->跳过整段def er的代码块->倒数第二行print,往下走遇到er()->跳回代码def er()继续执行到def san()跳过整段代码
    # 执行到san()->往回跳到def san():继续执行到 def si()跳到tsi()执行tsi函数,执行往后跳到->tsi()下的print->san()下的print
    # 7.递归,函数内部可以调用别的函数,如果在函数里调用自身我们就称为递归,递归理解起来比较费劲。。。递归遵循的两个原则:
    # (1) 回溯阶段必须要有一个明确地结束条件
    # (2) 每进入下一次递归时,问题的规模都应该有所减少(否则,单纯地重复调用自身是毫无意义的)
    # 示例1:
    # def test(x):
    # print(x)
    # if int(x /2) == 0:
    # return x
    # res=test(int(x/2))
    # return res
    # z=test(10)
    # print(z)
    # # 示例2:
    # renyuan=["张三","李四","王五","赵六","黄七"]
    # def wenlu(x):
    # person=x.pop(0)
    # if person =="黄七":
    # print("%s说:你所说的地方在XX大街,xx路",person)
    # else:
    # wenlu(person)
    # print("%s说:我也不知道我帮你问问吧",person)
    # return person
    # wenlu(renyuan)
     
  • 相关阅读:
    warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    Windows10+CLion+OpenCV4.5.2开发环境搭建
    Android解决部分机型WebView播放视频全屏按钮灰色无法点击、点击全屏白屏无法播放等问题
    MediaCodec.configure Picture Width(1080) or Height(2163) invalid, should N*2
    tesseract
    Caer -- a friendly API wrapper for OpenCV
    Integrating OpenCV python tool into one SKlearn MNIST example for supporting prediction
    Integrating Hub with one sklearn mnist example
    What is WSGI (Web Server Gateway Interface)?
    Hub --- 机器学习燃料(数据)的仓库
  • 原文地址:https://www.cnblogs.com/Centwei/p/9732884.html
Copyright © 2011-2022 走看看