*, **, * args, ** kwargs role

A: * the role of personal understanding on behalf of all

  def fun(a,b,c):

    print(a,b,c)

  l1=[1,2,3]

  l2 = [1,2,3,4]

  fun (* l1) ---- >>> 1 2 3 # can call to print out all the elements

  fun (* l2) ---- >>> wrong, you need to pay attention to the definition of fun when the three elements, and there are four elements l2 so wrong

Two: the role of **, on behalf of all personal understanding, but that is the object dictionary

def fun(a,b,c):
    print(a,b,c)
d={'b':2,'c':3}
fun(1,**d)  --->>> 1,2,3,
fun (** d) --- >>> error is defined when three fun element, and d is 2, the number does not match, the error

Three: * args role of the parameter packaged into a tuple call

  Fun DEF (X, Y, * args): 
      Print (X, Y, args) 
  Fun (1,2,3,4,5). 1 >>> 2 --- (. 3,. 4,. 5) 
will be 2 and x, Y matched to the remaining packaged into tuples * args

Four: ** kwargs role of the parameter packaged into dictionary

  def fun(a,**kwargs):
      print(a,kwargs)
  fun(1,b=2,c=3)  --->>> 1 {'b': 2, 'c': 3}
  fun (1, ** { 'b ': 2, 'c': 3}) --- >>> 1 { 'b': 2, 'c': 3} 
The remaining packed into the matching dictionary

Summary: *, ** when calling to use, * args, ** kwargs used in the definition of the function of time. And parameter order must be: Arg, * args, ** kwargs otherwise incorrect report


Guess you like

Origin www.cnblogs.com/jacker2019/p/11354779.html