Learn Python's summary of parameter passing

  • 2020-04-02 14:14:54
  • OfStack

So, as I said, we're done with the function. However, there are a lot of details about functions that are worth keeping in mind. Here's the explanation.

Parameter passing

Arguments to functions in python are passed in reference objects by assignment. The following summary provides an understanding of the flow of parameter passing by summarizing the common way in which function parameters are defined.

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

The most common way is to list a finite number of arguments separated by commas. When a function is called, the parameters are assigned in this order, and it is important to note that the name of the parameter is not important, but the location. Moreover, the quantity must be consistent, one-to-one correspondence. The first object (which may be a number, a string, etc.) corresponds to the first argument, the second to the second argument, and so on.


>>> def foo(p1,p2,p3):
...     print "p1==>",p1
...     print "p2==>",p2
...     print "p3==>",p3
...
>>> foo("python",1,["qiwsir","github","io"])    # One to one assignment
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)    # Watch for misinformation >>> foo("python",1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 3 arguments (4 given)    # requirements 3 Two parameters, actually placed 4 A, an error

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

This is a much more explicit way of assigning a certain parameter than the previous one, and it seems like it's not going to mess up. It's very explicit. It was rather like a radish to a pit.

I'm going to do it the same way, so I don't have to worry about the order.


>>> 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 certain parameters


>>> def foo(p1,p2=22,p3=33):    # Two parameters are set p2,p3 The default value of
...     print "p1==>",p1
...     print "p2==>",p2
...     print "p3==>",p3
...
>>> foo(11)     #p1=11 , other parameters are assigned by default
p1==> 11
p2==> 22
p3==> 33
>>> foo(11,222)     # In order, p2=222,p3 The default value remains
p1==> 11
p2==> 222
p3==> 33
>>> foo(11,222,333)  # Assign values in order
p1==> 11
p2==> 222
p3==> 333 >>> foo(11,p2=122)
p1==> 11
p2==> 122
p3==> 33 >>> foo(p2=122)     #p1 No default value, must be assigned, otherwise 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 uncertain number of parameters, in the parameter args before a *, note that only one yo.


>>> def foo(*args):         # Receive an indefinite number of data objects
...     print args
...
>>> foo("qiwsir.github.io") # In order to tuple The form receives even one
('qiwsir.github.io',)
>>> foo("qiwsir.github.io","python")
('qiwsir.github.io', 'python')

  We already had an example in the last lecture where we could mix it up with the previous one. It is not stated here.

Def foo * * args ()

The difference with this approach is that you must accept something like arg=val.


>>> def foo(**args):    # This way of receiving to dictionary Receive data objects in the form of
...     print args
... >>> foo(1,2,3)          # That's a mistake
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)    # And that's fine, because you have the key pair
{'a': 1, 'c': 3, 'b': 2}

  Let's take a comprehensive look at the execution order of the above four parameter passing methods


>>> 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 above example, whether the officer can see what is going on?


Related articles: