With the old Ziko Python summary parameters of the transfer _python

Source: Internet
Author: User
Tags stdin in python

As you can tell, the basic content of the function is complete. However, there are many details worth pondering. Here is the elaboration.

Passing of parameters

The parameters of a function in Python pass a reference object by assigning a value. The following summary summarizes the common function parameter definitions to understand the process of parameter passing.

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 parameter is assigned in order, and it is important to note that the name of the parameter is not important. Moreover, there must be a number of consistent and corresponding. The first object (possibly numeric, string, and so on) corresponds to the first argument, and the second corresponds to the second argument, so that it is not biased to the left or right.

Copy 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 call last):
File "<stdin>", line 1, in <module>
Typeerror:foo () takes exactly 3 arguments (1 given) #注意看报错信息

>>> foo ("Python", 1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Typeerror:foo () takes exactly 3 arguments (4 given) #要求3个参数, actually placed 4, the error


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

This way than the previous one to a more definite parameter of the assignment, it seems that this will not go wrong, very clear AH. Quite a radish meant for a pit.

Or the above function, the following way to assign values, you do not have to worry about the order problem.

Copy 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 ways, with default values for certain parameters

Copy Code code as follows:

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

p>

>>> foo (11,p2=122)
p1==>
p2==> 122
p3==>

>>> foo (p2=122) &nbs p;    #p1没有默认值, you must assign a value, otherwise the error
Traceback (most recent call last):
  File "<stdin>", line 1, In <module>
Typeerror:foo () takes at least 1 argument (1 given)

def foo (*args)

This method is suitable for the number of uncertain parameters, the parameter args before a *, note, only one yo.

Copy 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 examples in the previous lecture that can be used in combination with the previous one. Here is not to repeat.

def foo (**args)

The difference between this approach and the above is that it must receive a similar arg=val form.

Copy Code code as follows:

>>> def foo (**args): #这种方式接收, receiving data Objects in dictionary form
... print args
...

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

>>> foo (a=1,b=2,c=3) #这样就可以了 because 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 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 '}

Through the example above, does reader 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.