zoukankan      html  css  js  c++  java
  • Python--函数传参

    函数的一般参数(参数个数可确定):

     1 # x、y、z 为形参
     2 def func1(x, y, z):
     3     print("x=%s" % x)
     4     print("y=%s" % y)
     5     print("z=%s" % z)
     6     return 0
     7 # 1、2、3为实参(此处的1、2、3为位置参数,按顺序传给形参,要求实参形参一一对应),实参的数量不得多于形参的数量
     8 a = func1(1, 2, 3)
     9 # x=1、z=3、y=2为关键字参数,与形参位置无关
    10 b = func1(x=1, z=3, y=2)
    11 # 当同时有位置参数和关键字参数时,位置参数不能写在关键字参数后面,且参数需要一一对应
    12 c = func1(1, 2, z=3) #正确示例
    13 d = func1(x=1, 2, 3) #错误示例 SyntaxError: positional argument follows keyword argument
    14 e = func1(1, 2, x=2) #错误示例 func1() got multiple values for argument 'x'

    函数的默认参数:

    1 # 此处的port和dbname参数均为默认参数,当调用conn函数未传递给port参数值时,使用默认值3306,当传递参数给默认参数时,使用传入的参数
    2 def conn(host, port=2003, dbname="OSRDB"):
    3     print("jdbc:oscar://{host_ip}:{PORT}/{DBNAME}......".format(host_ip=host, PORT=port, DBNAME=dbname))
    4     return 0
    5 conn(host="127.0.0.1",dbname="ECRDB")
    6 
    7 # >>> jdbc:oscar://127.0.0.1:2003/ECRDB......

    函数的参数组:*args和**kwargs

     1 def test(x,y):
     2     print(x)
     3     print(y)
     4     return 0
     5 test(1,2,3) #报错,因为实参多于形参 TypeError: test() takes 2 positional arguments but 3 were given
     6 
     7 # 传递数量不固定的实参时,可以使用带*的形参,即参数组
     8 # 注意参数组要放在位置参数和关键字参数的后面
     9 
    10 # *args将多余的位置参数变成了一个元组
    11 def test1(a,*args):
    12     print(a) # 1
    13     print(args) # (2, 3, 4, 5)
    14     print(type(args)) # <class 'tuple'>
    15     print(args[1]) # 3
    16     return 0
    17 test1(1,2,3,4,5)
    18 
    19 # 实参*[列表]的使用
    20 def test2(a,*args):
    21     print(a) # 1
    22     print(args) # (2, 3, 4, 5, {'a': 1})
    23     print(type(args)) # <class 'tuple'>
    24     print(args[1]) # 3
    25     return 0
    26 test2(*[1,2,3,4,5],{"a":1}) # *[1,2,3,4,5] = 1,2,3,4,5
    27 
    28 # **kwargs将多余的关键字参数变成了一个字典
    29 def test3(a,**kwargs):
    30     print(a) # 1
    31     print(kwargs) # {'b': 2, 'c': 3, 'd': 4, 'e': 5}
    32     print(type(kwargs))  # <class 'dict'>
    33     print(kwargs["c"]) # 3
    34     return 0 35 test3(a=1,b=2,c=3,d=4,e=5)
    36 
    37 # 实参**字典的使用
    38 def test4(a,**kwargs):
    39     print(a) # 1
    40     print(kwargs) # {'b': 2, 'c': 3, 'd': 4}
    41     print(type(kwargs))  # <class 'dict'>
    42     print(kwargs["c"]) # 3
    43     return 0 44 test4(**{"a":1,"b":2,"c":3, "d":4}) # **{"a":1,"b":2,"c":3, "d":4} = {"a":1},{"b":2},{"c":3},{"d":4}
    45 
    46 
    47 # *args和**kwargs同时使用时,
    48 # 形参:*args要放在**kwargs前面,
    49 # 实参:位置参数放在关键字参数的前面
    50 def test5(name, age=18, *args, **kwargs):
    51     print(name) # tj
    52     print(age) # 2
    53     print(args) # (1, 3)
    54     print(kwargs) # {'b': 2, 'c': 18}
    55     return 0
    56 test5("tj", 2, 1, 3, b=2, c=18)
  • 相关阅读:
    软件工程实践总结-黄紫仪
    beta冲刺总结附(分工)-咸鱼
    beta冲刺总结-咸鱼
    beta冲刺7-咸鱼
    beta冲刺用户测评-咸鱼
    beta冲刺6-咸鱼
    beta冲刺5-咸鱼
    beta冲刺4-咸鱼
    beta冲刺3-咸鱼
    beta冲刺2-咸鱼
  • 原文地址:https://www.cnblogs.com/sunnytomorrow/p/12950432.html
Copyright © 2011-2022 走看看