程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Joking about Python * and**

編輯:Python

python And c++ Different , No pointer

1. Is a multiplication symbol

a=1
b=2
c=a*b
Output c Certainly 1×2=2. I believe this is not the focus of our concern .

2. Collect extra values in the list

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 .

3. Functions are involved ,* No. is used to collect parameters or allocate parameters .

Specific use , It's for reading * No. is used when a function is defined or called .( This sentence is very important !)

3.1 If you define a function ,* Represents collection parameters ,** Represents the collection of keyword parameters .

3.1.1 When defining a function , One * The situation of :

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 .

3.1.2 When defining a function , Two ** The situation of :

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 .

3.2 If you call a function ,* and ** Are used to allocate parameters

3.2.1 When you call a function , One * The situation of :

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 :

3.2.2 When you call a function , Two ** The situation of :

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 :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved