zoukankan      html  css  js  c++  java
  • 2、函数的核心内容

    1、函数对象

    2、函数嵌套

    3、名称空间和作用域

    4、闭包函数

    >>>>>>>>>>函数对象<<<<<<<<<<

    在python中、函数是第一对象,是第一等公民

    本质:函数可以当变量使用

    def func:   # func = 函数的内存地址
      print('from func')
    

    1、可以赋值

    f  =  func
    print( f )  = print(func) func 对应的内存地址
    f()  = 'form func'
    

    2、可以当作参数传给另外一个函数

    def func:   # func = 函数的内存地址
      print('from func')
    def foo(x) :       # 把 func 传给了   x
      print(x)
      x()
    foo(func)
    

    3、可以当作函数的返回值

    def func:   # func = 函数的内存地址
      print('from func')
    def foo(x):
      return x
    res = foo(func)
    print(x)>>>>>>> func 的内存地址
    

    4、可以当作容器类型的元素

    l = [ func,]
    print( l )  # func 的内存地址
    l[0]()   #  from func
    

    小例子:银行ATM机的框架 用函数搭建

    ##=全局程序定义阶段
    def withdraw():
      print(‘取款程序’)
    def tranfer():
      print('转账程序‘)
    def check_balabnce():
      print('查询余额程序’)
    def save():
      print('存款程序‘
    

    若想添加功能, 可直接在上面新建函数,添加代码实现

    ##=调用程序阶段
    func_dic = {
      '1':['提款’,withdraw],
      '2':['转账',tranfer],
      '3':['查询余额',check_balance]
      '4':['存款',save]
    while True:
      print('0 退出‘)
      for k ,v in func_dic .items():
        print(k,v[0])
      choice = input('>>>>:).strip()
      if choice == '0':
        break
      if choice in func_dic:
        func_dic[choice][1]()
      else:
        print('输入错误’)
    

    >>>>>>>>>>函数嵌套<<<<<<<<<<

    函数嵌套调用 小例子:比大小

    def max2(x,y)
      if x > y:
        return x
      else:
        return y
    def max4(a,b,c,d):
      res1 = max2(a,b)
      res2 = max2(res1,c)
      res3= max2(res2,d)
      return res3
    print(max4(11,22,99,56))>>>>>>>>99
    

    函数嵌套定义

    def f1():
      x = 10
      def f2():
        print('from f2)
      print(x)  >>>>> 10
      print(f2)
    f(1)
    print(x)>>>>不会找到
    print(f2)  >>>>不会找到
    

    >>>>>>>>>>名称空间和作用域<<<<<<<<<<

    1、namespaces名称空间 :存放名字 的地方
    
            内置名称空间:存放内置的名字。python自带的
                    生命周期:python解释器启动则产生,关闭则销毁
            全局名称空间:存放的是顶级的名字
                    生命周期:运行python文件时则产生,python文件运行完毕则销毁
            局部名称空间:存放的是函数内的名字 不调用函数就没有
                    生命周期:调用函数则产生,函数调用完毕则销毁
                x =10
                def foo(m)
                    m = 111
                    n = 222
                foo() >>>>>>111
                ##=核心:名字的访问优先级
                    基于当前所在位置向外查找
                        函数内---外层函数---全局---内置
    

    案例1

    len = 10
    def func():
          len = 20
          print(len)
    func()  >>>>20 
    print(len) >>>>>10
    

    案例2

    def f1():
          x = 111
          def f2():
                x=222
                print(x)
          f(2)
    x = 444
    f(1)>>>>>>>666
    

    =名称空间与作用域的关系是在定义阶段(扫描语法)确立的 与调用位置无关 调用时间无关

    x = 111
    def f1():            ##= f1()已经被定义了
          print(x)
    def f2():
          x = 222
          f1()
    f2()  >>>> 111
    
    x = 111
    def f1():
          print(x)
          x =222   >>>> 再print的下面 会报错
    f1()
    

    作用域

    全局作用域:内置名称空间+全局名称空间
            特点:全局存活,全局有效
    局部作用域:局部名称空间
            特点:临时存活 局部有效
    

    global 和 nonlocal 的用法

    l = []    l 是可变类型,所以可以直接添加或删除
    def func():
          l.append(111)
    func()>>>>>
    print(l)>>>>>['111'] 
    

    对于不可变类型

    x = 111
    def func():
          global x   全局更改 global
          x = 222
    func()
    print(x) >>>>>222
    
    x = 111
    def f1():
          x = 222
          def f2():
                nonlocal x  # 声明变量来自于外层函数,不是全局
                x = 333
          f2()
          print(x)
    f1()>>>333
    print(x)>>>111
    

    >>>>>>>>>>闭包函数<<<<<<<<<<

    闭:指的是该函数是定义在函数内的函数

    包:指的就算该函数引用了一个外层函数作用域的名字

    def outter():
          x =111
          def wrapper():
                print(x)
          return wrapper  此处不能加 ()
    f = outter()
    print(f)
    
    def foo():
          x =222
          f()
    foo()
    

    小案例 为函数体代码传参

    一、直接用参数传

    def wrapper(x):
          print(x)
    wrapper(111)
    wrapper(222)
    

    二、闭包函数传参

    def outter(x):
          def wrapper():
                print(x)
          return wrapper
    f1 = outter(111)
    f1()
    
    f2 = outter(222)
    f2()
    

    None of us is oerfect forever

  • 相关阅读:
    【LeetCode】Validate Binary Search Tree
    【LeetCode】Search in Rotated Sorted Array II(转)
    【LeetCode】Search in Rotated Sorted Array
    【LeetCode】Set Matrix Zeroes
    【LeetCode】Sqrt(x) (转载)
    【LeetCode】Integer to Roman
    贪心算法
    【LeetCode】Best Time to Buy and Sell Stock III
    【LeetCode】Best Time to Buy and Sell Stock II
    CentOS 6 上安装 pip、setuptools
  • 原文地址:https://www.cnblogs.com/liuyang521/p/14207863.html
Copyright © 2011-2022 走看看