• 总结参数的传递
    • 参数的传递
      • def foo(p1,p2,p3,…)
      • def foo(p1=value1,p2=value2,…)
      • def foo(*args)
        • def foo(**args)

    总结参数的传递

    就前面所讲,函数的基本内容已经完毕。但是,函数还有很多值得不断玩味的细节。这里进行阐述。

    参数的传递

    python中函数的参数通过赋值的方式来传递引用对象。下面总结通过总结常见的函数参数定义方式,来理解参数传递的流程。

    def foo(p1,p2,p3,…)

    这种方式最常见了,列出有限个数的参数,并且彼此之间用逗号隔开。在调用函数的时候,按照顺序以此对参数进行赋值,特备注意的是,参数的名字不重要,重要的是位置。而且,必须数量一致,一一对应。第一个对象(可能是数值、字符串等等)对应第一个参数,第二个对应第二个参数,如此对应,不得偏左也不得偏右。

    1. >>> def foo(p1,p2,p3):
    2. ... print "p1==>",p1
    3. ... print "p2==>",p2
    4. ... print "p3==>",p3
    5. ...
    6. >>> foo("python",1,["qiwsir","github","io"]) #一一对应地赋值
    7. p1==> python
    8. p2==> 1
    9. p3==> ['qiwsir', 'github', 'io']
    10. >>> foo("python")
    11. Traceback (most recent call last):
    12. File "<stdin>", line 1, in <module>
    13. TypeError: foo() takes exactly 3 arguments (1 given) #注意看报错信息
    14. >>> foo("python",1,2,3)
    15. Traceback (most recent call last):
    16. File "<stdin>", line 1, in <module>
    17. TypeError: foo() takes exactly 3 arguments (4 given) #要求3个参数,实际上放置了4个,报错

    def foo(p1=value1,p2=value2,…)

    这种方式比前面一种更明确某个参数的赋值,貌似这样就不乱子了,很明确呀。颇有一个萝卜对着一个坑的意味。

    还是上面那个函数,用下面的方式赋值,就不用担心顺序问题了。

    1. >>> foo(p3=3,p1=10,p2=222)
    2. p1==> 10
    3. p2==> 222
    4. p3==> 3

    也可以采用下面的方式定义参数,给某些参数有默认的值

    1. >>> def foo(p1,p2=22,p3=33): #设置了两个参数p2,p3的默认值
    2. ... print "p1==>",p1
    3. ... print "p2==>",p2
    4. ... print "p3==>",p3
    5. ...
    6. >>> foo(11) #p1=11,其它的参数为默认赋值
    7. p1==> 11
    8. p2==> 22
    9. p3==> 33
    10. >>> foo(11,222) #按照顺序,p2=222,p3依旧维持原默认值
    11. p1==> 11
    12. p2==> 222
    13. p3==> 33
    14. >>> foo(11,222,333) #按顺序赋值
    15. p1==> 11
    16. p2==> 222
    17. p3==> 333
    18. >>> foo(11,p2=122)
    19. p1==> 11
    20. p2==> 122
    21. p3==> 33
    22. >>> foo(p2=122) #p1没有默认值,必须要赋值的,否则报错
    23. Traceback (most recent call last):
    24. File "<stdin>", line 1, in <module>
    25. TypeError: foo() takes at least 1 argument (1 given)

    def foo(*args)

    这种方式适合于不确定参数个数的时候,在参数args前面加一个*,注意,仅一个哟。

    1. >>> def foo(*args): #接收不确定个数的数据对象
    2. ... print args
    3. ...
    4. >>> foo("qiwsir.github.io") #以tuple形式接收到,哪怕是一个
    5. ('qiwsir.github.io',)
    6. >>> foo("qiwsir.github.io","python")
    7. ('qiwsir.github.io', 'python')

    上一讲中已经有例子说明,可以和前面的混合使用。此处不赘述。

    def foo(**args)

    这种方式跟上面的区别在于,必须接收类似arg=val形式的。

    1. >>> def foo(**args): #这种方式接收,以dictionary的形式接收数据对象
    2. ... print args
    3. ...
    4. >>> foo(1,2,3) #这样就报错了
    5. Traceback (most recent call last):
    6. File "<stdin>", line 1, in <module>
    7. TypeError: foo() takes exactly 0 arguments (3 given)
    8. >>> foo(a=1,b=2,c=3) #这样就可以了,因为有了键值对
    9. {'a': 1, 'c': 3, 'b': 2}

    下面来一个综合的,看看以上四种参数传递方法的执行顺序

    1. >>> def foo(x,y=2,*targs,**dargs):
    2. ... print "x==>",x
    3. ... print "y==>",y
    4. ... print "targs_tuple==>",targs
    5. ... print "dargs_dict==>",dargs
    6. ...
    7. >>> foo("1x")
    8. x==> 1x
    9. y==> 2
    10. targs_tuple==> ()
    11. dargs_dict==> {}
    12. >>> foo("1x","2y")
    13. x==> 1x
    14. y==> 2y
    15. targs_tuple==> ()
    16. dargs_dict==> {}
    17. >>> foo("1x","2y","3t1","3t2")
    18. x==> 1x
    19. y==> 2y
    20. targs_tuple==> ('3t1', '3t2')
    21. dargs_dict==> {}
    22. >>> foo("1x","2y","3t1","3t2",d1="4d1",d2="4d2")
    23. x==> 1x
    24. y==> 2y
    25. targs_tuple==> ('3t1', '3t2')
    26. dargs_dict==> {'d2': '4d2', 'd1': '4d1'}

    通过上面的例子,看官是否看出什么名堂了呢?