zoukankan      html  css  js  c++  java
  • Python函数和lambda表达式

    可以通过python内置的help()函数查看其它函数的帮助文档

    函数说明文档:放置在函数声明之后,函数体之前,说明文档可以通过help(函数名)或者函数名.__doc__查看

    函数可以返回多个值,函数返回时多个值被封装成一个元组,调用时可以直接使用多个变量接收函数的返回值。

    形参

    def girth(width,height):
        print("",width)
        print("height:",height)
        return 2*(width+height)
    
    
    print(girth(1.2,2.3)) #根据位置传入参数
    print(girth(width=1.2,height=2.3)) #关键字参数
    print(girth(height=2.3,width=1.2)) #使用关键字参数时可交换位置
    print(girth(1.2,height=2.3)) #部分使用位置参数,部分使用关键字参数

    参数收集:python可以定义数量可变的参数,在形参前面加*,意味着该参数可接收多个值,多个值被当做元组传入。可变形参可以位于形参列表的任意位置。

    def test(a,*books):
        print(books)
        print(a)
    
    test(5,"C++","C#","Python")  #('C++', 'C#', 'Python')
                                                # 5
    View Code

    关键字参数收集:在形参前面加两个星号,则python会将关键字参数收集为字典

    def test(x,y,*books,**scores):
        print(x,y)
        print(books)
        print(scores)
    
    test(1,2,'C++','Python',math=90,English=89)
    # 1 2
    # ('C++', 'Python')
    # {'math': 90, 'English': 89}
    View Code

    python中的函数参数传递是值传递

    局部函数:函数定义在其他函数的封闭函数体内,如果封闭函数没有返回局部函数,则局部函数只能在封闭函数内调用;如果封闭函数将局部函数返回且程序使用了变量接收封闭函数的返回值,则局部函数的作用域会被扩大。

    def get_math_func(type,nn):
        def square(n):
            return n*n
        def cube(n):
            return n*n*n
        def factorial(n):
            result=1
            for i in range(2,n+1):
                result*=i
            return result
        #调用局部函数
        if type=='square':
            return square(nn)
        elif type=='cube':
            return cube(nn)
        else:
            return factorial(nn)
    
    print(get_math_func('square',5)) #25
    print(get_math_func('cube',6)) #216
    print(get_math_func('',5)) #120
    View Code

    python所有函数都是function对象,可以吧函数赋值给变量,作为函数参数以及返回值。

    #函数fn作为参数
    def map(data,fn):
        result=[]
        for e in data:
            result.append(fn(e))
        return result
    
    def square(x):
        return x*x
    
    data=tuple(range(10))
    print(map(data,square)) #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

    lambda表达式

    lambda表达式的语法格式为   lambda 参数列表:表达式,参数列表使用逗号分割

    lambda表达式实质上就是匿名的、单行函数体的函数

    def square(n):
        return n*n
    
    def cube(n):
        return n*n*n
    
    def factorial(n):
        result=1
        for i in range(2,n+1):
            result *=i
        return result
    
    def map(data,fn):
        result=[]
        for e in data:
            result.append(fn(e))
        return result
    
    data=list(range(8))
    print(map(data,square)) # [0, 1, 4, 9, 16, 25, 36, 49]
    print(map(data,lambda x: x*x if x%2==0 else 0)) # [0, 0, 4, 0, 16, 0, 36, 0]
    View Code
  • 相关阅读:
    Android OpenGL ES 2.0 (四) 灯光perfragment lighting
    Android OpenGL ES 2.0 (五) 添加材质
    冒泡排序函数
    javascript object 转换为 json格式 toJSONString
    Liunx CentOS 下载地址
    jquery 图片切换特效 鼠标点击左右按钮焦点图切换滚动
    javascript 解析csv 的function
    mysql Innodb Shutdown completed; log sequence number解决办法
    Centos 添加 yum
    javascript 键值转换
  • 原文地址:https://www.cnblogs.com/larry-xia/p/9092247.html
Copyright © 2011-2022 走看看