704人阅读 评论(1)

# 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})

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：116243次
• 积分：2104
• 等级：
• 排名：第18292名
• 原创：89篇
• 转载：2篇
• 译文：3篇
• 评论：29条
评论排行
最新评论