The transfer of summary parameters to old Ziko python

Source: Internet
Author: User
As mentioned earlier, the basic content of the function is complete. However, there are many details that are worth pondering over the function. This is explained here.

Passing of parameters

The parameters of a function in Python pass a reference object in a way that assigns a value. The following summarizes the flow of parameter passing by summarizing common function parameter definition methods.

def foo (p1,p2,p3,...)

This is the most common way to list a finite number of parameters, separated by commas. When calling a function, the parameters are assigned in order, and it is important to note that the name of the parameter is not important, but that it is the position. Moreover, the number must be consistent and correspond. The first object (possibly a numeric value, a string, and so on) corresponds to the first parameter, and the second corresponds to the second parameter, so it must not be left nor biased to the right.
Copy the Code code as follows:


>>> def foo (P1,P2,P3):
... print "p1==>", p1
... print "p2==>", p2
... print "p3==>", p3
...
>>> foo ("Python", 1,["Qiwsir", "GitHub", "IO"]) #一一对应地赋值
P1==> python
P2==> 1
p3==> [' Qiwsir ', ' GitHub ', ' io ']

>>> foo ("Python")
Traceback (most recent):
File " ", line 1, in
Typeerror:foo () takes exactly 3 arguments (1 given) #注意看报错信息

>>> foo ("python", +/--)
Traceback (most recent):
File " ", line 1, in
Typeerror:foo () takes exactly 3 arguments (4 given) #要求3个参数, actually placed 4, error


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

This way is more specific than the previous assignment of a parameter, it seems that this is not a mess, very clear AH. Quite a radish is meant for a pit.

Or the above function, assign a value in the following way, without worrying about the order problem.
Copy the Code code as follows:


>>> foo (p3=3,p1=10,p2=222)
P1==> 10
P2==> 222
P3==> 3

You can also define parameters in the following way, with default values for some parameters
Copy the Code code as follows:


>>> def foo (p1,p2=22,p3=33): #设置了两个参数p2, default value for P3
... print "p1==>", p1
... print "p2==>", p2
... print "p3==>", p3
...
>>> foo (one) #p1 = 11, other parameters are assigned by default
P1==> 11
p2==> 22
P3==> 33
>>> foo (11,222) #按照顺序, P2=222,P3 still maintains the original default value
P1==> 11
P2==> 222
P3==> 33
>>> foo (11,222,333) #按顺序赋值
P1==> 11
P2==> 222
P3==> 333

>>> foo (11,p2=122)
P1==> 11
P2==> 122
P3==> 33

>>> foo (p2=122) #p1没有默认值, must be assigned value, otherwise error
Traceback (most recent):
File " ", line 1, in
Typeerror:foo () takes at least 1 argument (1 given)

def foo (*args)

This method is suitable for the number of uncertain parameters when the parameter args before adding a *, note, only one yo.
Copy the Code code as follows:


>>> def foo (*args): #接收不确定个数的数据对象
.. print args
...
>>> foo ("Qiwsir.github.io") #以tuple形式接收到, even if it is a
(' Qiwsir.github.io ',)
>>> foo ("Qiwsir.github.io", "Python")
(' Qiwsir.github.io ', ' python ')

There are already examples in the previous lecture that can be used in combination with the previous one. Do not repeat here.

def foo (**args)

The difference between this approach and the above is that it must receive a similar arg=val form.
Copy the Code code as follows:


>>> def foo (**args): #这种方式接收 to receive data objects in the form of dictionary
.. print args
...

>>> foo (#这样就报错了)
Traceback (most recent):
File " ", line 1, in
Typeerror:foo () takes exactly 0 arguments (3 given)

>>> foo (a=1,b=2,c=3) #这样就可以了 because of a key-value pair
{' A ': 1, ' C ': 3, ' B ': 2}

Here's a comprehensive look at the execution order of the above four parameter passing methods
Copy the Code code as follows:


>>> def foo (X,y=2,*targs,**dargs):
... print "x==>", X
... print "y==>", y
... print "targs_tuple==>", Targs
... print "dargs_dict==>", Dargs
...

>>> foo ("1x")
X==> 1x
Y==> 2
Targs_tuple==> ()
dargs_dict==> {}

>>> foo ("1x", "2y")
X==> 1x
Y==> 2y
Targs_tuple==> ()
dargs_dict==> {}

>>> foo ("1x", "2y", "3t1", "3t2")
X==> 1x
Y==> 2y
Targs_tuple==> (' 3t1 ', ' 3t2 ')
dargs_dict==> {}

>>> foo ("1x", "2y", "3t1", "3t2", d1= "4d1", d2= "4d2")
X==> 1x
Y==> 2y
Targs_tuple==> (' 3t1 ', ' 3t2 ')
dargs_dict==> {' D2 ': ' 4d2 ', ' D1 ': ' 4d1 '}

By the above example, does crossing see anything?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.