第二十六篇 函数的嵌套

目录

第二十六篇 函数的嵌套

一、函数的嵌套定义

  • 函数内部定义的函数,无法在函数外部使用。(只能在它所属的函数那一层或更深层进行调用)
form cmath import pi # 3.14

def circle(r,action):
    if action == 'p':
        def perimeter():  #周长
            res = 2*pi*r
    elif action == 'a':
        def area():
            res = pi*r**2
    else:
        res = 'error'
    return res    

二、函数的嵌套调用


def max(x, y):
    if x > y:
        return x
    else:
        return y


def max1(a, b, c, d):
    res1 = max2(a, b)
    res2 = max2(res1, c)
    res3 = max2(res2, d)
    return res3

max1(20,59,100,0)

猜你喜欢

转载自www.cnblogs.com/itboy-newking/p/10953493.html