• Python函数的参数小结


    参数转化:

     1 def fun(a, b, c):
     2     print('a=', a)
     3     print('b=', b)
     4     print('c=', c)
     5 
     6 fun(10, 20, 30)
     7 lst = [10, 20, 30]
     8 fun(*lst) #函数调用时,将列表中的每个元素都转换为位置实参传入
     9 print('----------')
    10 fun(a=100, b=200, c=300)
    11 dic = {'a':100, 'b':200, 'c':300}
    12 fun(**dic)
     1 def fun(a, b=10):
     2     print('a=', a)
     3     print('b=', b)
     4 
     5 def fun2(*args):
     6     print(args)
     7 def fun3(**args):
     8     print(args)
     9 
    10 fun2(10, 20, 30, 40)
    11 fun3(a=11, b=22, c=33, d=44)
    12 
    13 def fun4(a, b, c, d):
    14     print('-----fun4------')
    15     print('a=', a)
    16     print('b=', b)
    17     print('c=', c)
    18     print('d=', d)
    19 fun4(10, 20, 30, 40)
    20 fun4(a=40, b=30, c=20, d=10)
    21 fun4(10, 20, c=40, d=30)
    22 # fun4(a=20, b=10, 30, 40) SyntaxError: positional argument follows keyword argument
    23 
    24 '''如何要求cd必须为关键字传参?'''
    25 def fun5(a, b, *, c, d): # *之后,必须是关键字传参
    26     print('-----fun5------')
    27     print('a=', a)
    28     print('b=', b)
    29     print('c=', c)
    30     print('d=', d)
    31 fun5(a=40, b=30, c=20, d=10)
    32 fun5(10, 20, c=40, d=30)
    33 
    34 '''函数定义时,形参的顺序问题'''
    35 def fun7(a, b, *, c, d, **args):
    36     pass
    37 def fun6(*args1, **args2):
    38     pass
    39 def fun8(a, b=10, *args1, **args2):
    40     pass

  • 相关阅读:
    O(n^2)的排序方法
    99乘法表
    excel 转 csv
    批量关闭 excel
    tomcat 加入服务
    文件打包 zip
    字符串转换
    List数组种删除数据
    mybatis 批量上传
    sql server 查询表字段及类型
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/15939299.html
Copyright © 2020-2023  润新知