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

Some examples of the use of * in Python function parameters

編輯:Python

List of articles

          • 1. Represents multiple parameters
          • 2. When you call a function , One `*` No, No ` decompression ` parameter list
          • 3. Use one of the function parameters at the same time `*` and `**`

Reference resources : https://www.cnblogs.com/mrdoghead/p/12014270.html

1. Represents multiple parameters

Add before parameter * Number , It means that the number of parameters is more than one , With an asterisk * The parameter passed in by the function of parameter is stored as a tuple (tuple), Take two * The sign indicates the dictionary (dict)

for example :

def exam0(par0, *par1):
print(par0)
print(par1)
exam1(1,2,3,4)
# 1
# (2,3,4)
def exam1(par0, **par1):
print par0
print par1
exam2(1,a=2,b=3)
# 1
# {a:2, b:3}
2. When you call a function , One * No, No decompression parameter list
def exam3(par0, par1):
print(par0, par1)
args = [1, 2]
exam3(*args)
# 1 2
3. Use one of the function parameters at the same time * and **
def exam4(a, b=10, *args, **kwargs):
print(a)
print(b)
print(args)
print(kwargs)
exam4(1, 2, 3, 4, e=5, f=6, g=7)
# 1
# 2
# 3 4
# {'e': 5, 'g': 7, 'f': 6}

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