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

    函数嵌套的定义

    函数内部定义的函数,无法在函数外部使用内部定义的函数

    def f1():
        def f2():
            print('from f2')
        f2()
    f2()    ## NameError: name 'f2' is not defined
    
    def f1():
        def f2():
            print('from f2')
        f2()
    f1()  #from f2
    

    定义一个circle函数,通过传参的形式得到园的面积或者周长

    import cmath
    def circle(r,action):
        if action=='area':
            return cmath.pi*r**2
        elif action=='length':
            return 2*cmath.pi*r
    area=circle(3,'area')
    print(area)
    length=circle(3,'length')
    print(length)
    

    28.274333882308138
    18.84955592153876

    import cmath
    def circle(r, action):
        def area():
            return cmath.pi * r ** 2
        def length():
            return 2*cmath.pi*r
        if action=='area':
            return area()
        else:
            return length()
    area=circle(3,'area')
    print(area)
    length=circle(3,'length')
    print(length)
    

    28.274333882308138
    18.84955592153876

    #比较两个值
    def self_max(x, y):
        if x > y:
            return x
        return y
    
    
    res = self_max(10, 20)
    print(res)
    

    20

    #比较四个值
    def self_4_max(x, y, z, c):
        res1 = max(x, y)
        res2 = max(z, c)
        res= max(res1, res2)
    
        return res
    
    
    res = self_4_max(10, 20, 1, 100)
    print(res)
    

    100

  • 相关阅读:
    二项式反演
    快速沃尔什变换
    springMVC的form标签
    springMVC的拦截器配置
    RESTful使用方法
    springMVC数据绑定
    使用spring框架自带的字符拦截器
    将idea中的项目上传至github
    springMVC的使用方式
    springMVC的概述
  • 原文地址:https://www.cnblogs.com/aden668/p/11329310.html
Copyright © 2011-2022 走看看