• python基础(十)函数式编程


    '''
    编程方式分为三种,分别是面向对象class、面向过程def、函数式编程def
    在python中,对面向对象或函数式编程不做区分
    '''
    #函数式编程语法
    def func1():
    '''注释'''
    print('函数式编程') #语句
    return 0
    #面向过程语法
    def func2():
    '''注释'''
    print('面向过程') #语句

    '''函数式编程的返回值'''
    def test1():
    print('in she test1')
    def test2():
    print('in she test2')
    return 0
    def test3():
    print('in she test3')
    return 1,'Clyde',['Clyde','Lili'],{'name':'Clyde'}
    x=test1() #打印结果:in she test1
    y=test2() #打印结果:in she test2
    z=test3() #打印结果:in she test3
    print(x) #打印结果:None
    print(y) #打印结果:0
    print(z) #打印结果:(1, 'Clyde', ['Clyde', 'Lili'], {'name': 'Clyde'});所有结果包含在元祖中

    '''编程式函数调用'''
    def test(x,y):
    print(x)
    print(y)
    test(1,2) #位置参数调用
    test(y=3,x=4) #关键字参数调用
    #默认参数
    def test1(soft1,soft2=True): #默认参数要在最后,否则报错
    print(soft1)
    print(soft2)
    test1(True,False)
    #接收多个参数
    def test2(*a):
    print(a)
    test2(1,2,3) #打印结果:(1, 2, 3) 把多个位置参数变成元祖
    test2(*[3,4,5]) #打印结果:(3, 4, 5) a=tuple([3,4,5])
    def test3(**b):
    print(b)
    print(b['name']) #打印结果:Clyde
    test3(name='Clyde',age='25') #打印结果:{'name': 'Clyde', 'age': '25'} 把多个关键字参数转换成字典
    test3(**{'name':'Clyde','age':'25'}) #打印结果:{'name': 'Clyde', 'age': '25'}
  • 相关阅读:
    初始化ArrayList的两种方法
    MySQL最大连接数设置
    页面按钮的语义和可访问性
    H5+App开发框架汇总
    JS使用模板快速填充HTML控件数据
    Meta标签中的format-detection属性及含义
    java中@Qualifier("string")是什么用法
    MySQL 当记录不存在时insert,当记录存在时update
    美国40岁以上的程序员在干啥
    老程序员都去哪了?
  • 原文地址:https://www.cnblogs.com/zbvc/p/12955478.html
Copyright © 2020-2023  润新知