python And c++ Different , No pointer
a=1
b=2
c=a*b
Output c Certainly 1×2=2. I believe this is not the focus of our concern .
In this way, it is not necessary to ensure that the number of hedges is the same as the number of variables . for instance :
a,b,c=[1,2,3,4]
c Ahead No. is mainly used to collect parameters . So output a, yes 1; Output b, yes 2, Output c, yes [3,4]. Notice that the list is collected here .
Specific use , It's for reading * No. is used when a function is defined or called .( This sentence is very important !)
At this time, if the following method is used to call myprint
So the function is to call all the values provided , Put it in a tuple .
Follow up 2 There is a difference between the two ,2 Here are the extra parameters in the collection list , Here is the collection of good parameters , Put them into tuples .
In this case , In the parameter of function definition *params Back , You'd better not add any other formal parameters , For example, you define it as def myprint(*params,x) , When called myprint(1,2,3), You're going to report a mistake . Because of this python I can't tell which data is for params Of . If you have to define it that way , But when it is called , What must be displayed indicates which value is given to x Of . such as myprint(1,2,x=3), Call in this way so that there is no error .
For the previous parameter is *params The situation of ,myprint Keyword parameters cannot be passed in . What do you mean , Is still defined in this way :
This is wrong :
because * No. does not collect keyword parameters . Therefore, it needs to be modified in the following ways , Use two asterisks :
This call myprint2 There will be no problem :
Get a dictionary . The order of the elements in the dictionary depends on your environment .
for example , It's just that print function
Now there are two formal parameters , But can I just pass in one parameter ?
Yes ,“ Assign parameters when calling a function ” Follow “ Collect parameters when defining a function ”, In turn !
Suppose you have a tuple :
params=(1,2)
You can call myprint function :
myprint(*params)
You can output :
And above 3.1.2 It's like , Is the assignment dictionary Of .
This time params It's a dictionary :
params={‘x’:1,‘y’:2}
You can call myprint function :
myprint(**params)
You can output :