1.带默认值的参数

def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
print("Common args", common)
print("Default String", defaultStr)
print("Default Number", defaultNum)   

defaultValueArgs("Test")

#Common args Test
#Default String default
#Default Number 0

defaultValueArgs("Test", "Str", defaultNum = 1)

#Common args Test
#Default String Str
#Default Number 1

defaultValueArgs("Test",  defaultNum = 1)

#Common args Test
#Default String default
#Default Number 1

2.带一个星号（*）的函数参数

def singalStar(common, *rest):
print("Common args: ", common)
print("Rest args: ", rest)

singalStar("hello")

#Common args:  hello
#Rest args:  ()

singalStar("hello", "world", 000)

#Common args:  hello
#Rest args:  ('world', 0)

singalStar("hello", ("world", 000))

#Common args:  hello
#Rest args:  (('world', 0),)

singalStar("hello", *("world", 000))
#     singalStar("hello", *("world", 000), "123")    #error

#Common args:  hello
#Rest args:  ('world', 0)

3.带两个星号（*）的函数参数

def doubleStar(common, **double):
print("Common args: ", common)
print("Double args: ", double)

doubleStar("hello")

#Common args:  hello
#Double args:  {}

doubleStar("hello", "Test", 24)       #error
doubleStar("hello", x = "Test", y = 24)

#Common args:  hello
#Double args:  {'y': 24, 'x': 'Test'}

#doubleStar("hello", **{"name": "Test", "age": 24}, {"name": "Test2", "age": 24})    #error
#doubleStar("hello", {"name": "Test", "age": 24})    #error
doubleStar("hello", **{"name": "Test", "age": 24})

#Common args:  hello
#Double args:  {'name': 'Test', 'age': 24}

def singalAndDoubleStar(common, *single, **double):
print("Common args: ", common)
print("Single args: ", single)
print("Double args: ", double)

4.总结

1. 默认值函数参数。这种函数定义时，第一个有默认值的参数后的每一个参数都必须提供默认值。传参时，可以直接传参，也可以以“默认值参数名=value”的形式传参。
2. 单星号函数参数。单星号函数参数接收的参数组成一个元组。
3. 双星号函数参数。双星号函数参数接收的参数组成一个字典。

def singalStar(common, *rest):
print("Common args: ", common)
print("Rest args: ", rest)

def doubleStar(common, **double):
print("Common args: ", common)
print("Double args: ", double)

def singalAndDoubleStar(common, *single, **double):
print("Common args: ", common)
print("Single args: ", single)
print("Double args: ", double)

def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
print("Common args", common)
print("Default String", defaultStr)
print("Default Number", defaultNum)

if __name__ == "__main__":
defaultValueArgs("Test")
defaultValueArgs("Test", "default", defaultNum = 1)

singalStar("hello")
singalStar("hello", "world", 000)
singalStar("hello", ("world", 000))
singalStar("hello", ("world", 000), {123: 123})
singalStar("hello", *("world", 000))
#     singalStar("hello", *("world", 000), "123")    #error

doubleStar("hello")
doubleStar("hello", x = "Test", y = 24)
doubleStar("hello", **{"name": "Test", "age": 24})
#     doubleStar("hello", {"name": "Test", "age": 24})    #error

singalAndDoubleStar("hello")
singalAndDoubleStar("hello", "world", 000)
singalAndDoubleStar("hello", "world", 000, {"name": "Test", "age": 24})
singalAndDoubleStar("hello", "world", 000, **{"name": "Test", "age": 24})
singalAndDoubleStar("hello", ("world", 000), {"name": "Test", "age": 24})
#     singalAndDoubleStar("hello", *("world", 000), {"name": "Test", "age": 24})      #error
singalAndDoubleStar("hello", *("world", 000), **{"name": "Test", "age": 24})

• 本文已收录于以下专栏：

举报原因： 您举报文章：Python带星号*的函数参数 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)