Home > other >  Python and keyword arguments why an error?
Python and keyword arguments why an error?

Time:11-20

Def fuction (c, a, b=1, * * * d) :
Print (a, b, c, d)


List1=[1, 2, 3, 4, 5]
Dic={' q: 0, 'w' : 0, 'e' : 0}

List1 fuction (1, b=2, *, * * dic)

Error:
List1 fuction (1, b=2, *, * * dic)
TypeError: fuction () got multiple values for argument 'b'


B=2 at this time why can't cover b=1

CodePudding user response:

Does anyone reply to me

CodePudding user response:

You should pass arguments: fuction (1, b=2, c=list1, d=dic)

You watch the results suggest that preach, TypeError: fuction () got multiple values for argument 'b', knew too much of the incoming parameter
Because * is to parse it out
Actual fuction (1, b=2, * list1, * * dic) of the incoming fuction is (1, b=2, 1, 2, 5-tetrafluorobenzoic,... )

* and * * of the parameter is said it is a variable parameter (i.e., the length is not necessarily), * * said it is passed in the form of key-value pairs, don't need to add in front when the incoming *
Also should be in front of the variable parameter has a default parameters, according to the priority judging relationship, so this situation you must specify the c=list1, d=dic

Can find parameters related blog know about the function and transfer rules

CodePudding user response:

I think I should is the compiler the variable-length argument first value read into a default argument, so I suggest fuction () got multiple values for argument 'b'

Definition of function, put the variable-length argument to the default parameters, can achieve your requirements,
The following code in python3.7.4 test by

 
Def fuction (a, c, b=1, * * d) :
Print (a, b, c, d)


List1=[1, 2, 3, 4, 5]
Dic={' q: 0, 'w' : 0, 'e' : 0}

List1 fuction (1, b=2, *, * * dic)

CodePudding user response:

If your function definition, general usage is

 fuction (1, 2,4,5,6,7, b=12, f=101, g=102, h=10000) 


This is the * * kwargs transfer method,
  • Related