zoukankan      html  css  js  c++  java
  • 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'}
  • 相关阅读:
    旋转数组中的最小数字
    二叉树的遍历:先序,中序,后序,递归,非递归,层次遍历
    重建二叉树
    Combination Sum II
    Combination Sum
    红黑树
    python 时间模块
    docker 常用命令
    python request 和requests 的区别
    python 面试题1
  • 原文地址:https://www.cnblogs.com/zbvc/p/12955478.html
Copyright © 2011-2022 走看看