Python:Python函数的参数传递

函数四种传值:1、必选;2、默认;3、可选;4、关键字;

第1、2种为常见传参;

3、可选传参:

    使用“functions(*t)”其中“*t”为可选参数,数量不限制,返回对象为元组(tuple);

    EG:def printFunction(*t):

                print (t)

            printFunction('abc','def')  #输出"('abc,'def')" ;

4、关键字传参:

    使用“functions(**d)”其中“*d”为关键字参数,数量不限,但是需要为指定形式,返回对象为(dict);

    EG:def printFunction(**d):

                print (d)

            printFunction(A = 'abc',B = 'def')  #输出"{ 'A' : 'abc, 'B' : 'def' )" ;

PS:将元组拆成可选参数 or 将字典拆成关键字传参

--------------------start------------------------

    tuple1 = (1,2,3)

    def function(x,y,z)

    function(*tuple1)

--------------------分割线------------------------

    dict1 = {'name' : 'Alice' ,'phone' : '123456'}

    def function(name = None,phont  = None)

    function(**dict1) 

---------------------end---------------------------

你可能感兴趣的:(Python)