函数对象 函数嵌套

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

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()#报错

原文地址:https://www.cnblogs.com/frank007/p/9707169.html