zoukankan      html  css  js  c++  java
  • 函数对象 函数嵌套

    一、函数是第一类对象,即函数可以当做数据传递

    1.可以被引用

    2.可以当做参数传递

    3.返回值可以是函数

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

    二、利用该特性,优雅的取代多分支的if

    def foo():

      print(‘foo’)

    def bar():

      print(‘bar’)

    dic={

      ‘foo’:foo,

      ‘bar’:bar,  

    }

    while True:

      chice =input(‘>>: ’).strip()  

      if choice in dic:

        dic[ choice]()

    ****************************************************************************************************、

    一、函数的嵌套调用

    def max(x,y):

      return x  if x >y  else  y

    def max4(a,b,c,d):

      res1=max(a,b)

      res2=max(res1,c)

      res3=max(res2,d)

      return res3

    print(max(1,2,3,4))

    二、函数的嵌套定义

    def f1():
      def f2():

        def f3():

          print('from f3')    

        f3()

      f2()

    f1()

    f3()#报错

  • 相关阅读:
    php-instanceof运算符
    windows10-seaslog安装笔记
    [类和对象]1 封装 调用成员函数
    [C++] 拓展属性
    [C++] 引用详解
    [C++] Const详解
    ROS 常用
    win10 ubuntu16双系统安装教程
    [0] OpenCV_Notes
    Ubuntu16.04安装openCV的问题集合
  • 原文地址:https://www.cnblogs.com/frank007/p/9707169.html
Copyright © 2011-2022 走看看