Python中函数传递和接收参数

总结:

(1)函数实参中含有*号,如*contents,则表示把列表变量或者元组变量contents拆开作为位置参数传入函数.如果函数实参中含有**号,如**contents,则表示把字典变量contents拆开作为关键字参数传入函数,字典的键和值分别作为函数的形参和实参
(2)函数形参中含有*号,如*contents,则表示该函数可以接受任意数量的位置实参,在函数内部,变量contents是作为元组tuple来对待.如果函数形参中含有**号,如**contents,那么表示该函数可以接受任意数量的关键字实参,在函数内部,变量contents是作为字典来对待的.

函数从列表、元组和字典中接收参数:

# 函数接收列表、元组和字典中的参数

def printInfo(name,gender,age,weight):
    print("name:",name)
    print("gender:",gender)
    print("age:",age)
    print("weight:",weight)

print("示例1".center(50,"-"))
ls = ["甲","男",25,100]
printInfo(*ls)


print("示例2".center(50,"-"))
tp = ("乙","女",44,150)
printInfo(*tp)

print("示例3".center(50,"-"))
dict1 = {"name":"丙","gender":"Male","age":55,"weight":78}
printInfo(**dict1)

print("示例4".center(50,"-"))
dict2 = dict(name="丁",gender="Female",age=66,weight=847)
printInfo(**dict2)

控制台下的输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher' '54332' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹\test.py'
-----------------------示例1------------------------
name: 甲
gender: 男
age: 25
weight: 100
-----------------------示例2------------------------
name: 乙
gender: 女
age: 44
weight: 150
-----------------------示例3------------------------
name: 丙
gender: Male
age: 55
weight: 78
-----------------------示例4------------------------
name: 丁
gender: Female
age: 66
weight: 847
PS C:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹> 

形参中的*号和**号的作用:

# 函数接收列表

def printInfo(name,gender,age,weight):
    print("name:",name)
    print("gender:",gender)
    print("age:",age)
    print("weight:",weight)

# print("示例1".center(50,"-"))
# ls = ["甲","男",25,100]
# printInfo(*ls)

# 接收元组
def getFunc1(*contents):
    print("传入的参数的类型是:",type(contents))
    for city in contents:
        print(city)

def getFunc1_(contents):
    print("传入的参数的类型是:",type(contents))
    for city in contents:
        print(city)

# 接收字典
def getFunc2(**contents):
    print("传入的参数的类型是:",type(contents))
    for k,v in contents.items():
        print("关键字是:{0} 值是:{1}".format(k,v))


print("示例1A".center(50,"-"))
ls = ["上海","北京","重庆","天津"]
getFunc1(ls)

print("示例1B".center(50,"-"))
ls = ["上海","北京","重庆","天津"]
getFunc1(*ls)

print("示例1C".center(50,"-"))
ls = ["上海","北京","重庆","天津"]
getFunc1_(ls)

print("示例1D".center(50,"-"))
getFunc1("北京","上海","广州","深圳")


print("示例2A".center(50,"-"))
tp = ("舟山","衢州","金华","杭州","嘉兴")
getFunc1(tp)

print("示例2B".center(50,"-"))
tp = ("舟山","衢州","金华","杭州","嘉兴")
getFunc1(*tp)

print("示例3".center(50,"-"))
getFunc2(heilongjiang="哈尔滨",zhejiang="杭州",福建="福州",山东="济南",江苏="南京")

print("示例4".center(50,"-"))
dic = dict(江苏="南京",zhejiang="杭州",hebei="石家庄")
# print(dic)
getFunc2(**dic)

print("示例5".center(50,"-"))
dic = {'江苏':"南京",'zhejiang':"杭州","hebei":"石家庄"}
# print(dic)
getFunc2(**dic)

控制台下的输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher' '55714' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\新建文
件夹\test.py'
-----------------------示例1A-----------------------
传入的参数的类型是: <class 'tuple'>
['上海', '北京', '重庆', '天津']
-----------------------示例1B-----------------------
传入的参数的类型是: <class 'tuple'>
上海
北京
重庆
天津
-----------------------示例1C-----------------------
传入的参数的类型是: <class 'list'>
上海
北京
重庆
天津
-----------------------示例1D-----------------------
传入的参数的类型是: <class 'tuple'>
北京
上海
广州
深圳
-----------------------示例2A-----------------------
传入的参数的类型是: <class 'tuple'>
('舟山', '衢州', '金华', '杭州', '嘉兴')
-----------------------示例2B-----------------------
传入的参数的类型是: <class 'tuple'>
舟山
衢州
金华
杭州
嘉兴
-----------------------示例3------------------------
传入的参数的类型是: <class 'dict'>
关键字是:heilongjiang 值是:哈尔滨
关键字是:zhejiang 值是:杭州
关键字是:福建 值是:福州
关键字是:山东 值是:济南
关键字是:江苏 值是:南京
-----------------------示例4------------------------
传入的参数的类型是: <class 'dict'>
关键字是:江苏 值是:南京
关键字是:zhejiang 值是:杭州
关键字是:hebei 值是:石家庄
-----------------------示例5------------------------
传入的参数的类型是: <class 'dict'>
关键字是:江苏 值是:南京
关键字是:zhejiang 值是:杭州
关键字是:hebei 值是:石家庄
PS C:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹>

同时使用*形参和**形参:



# def printStuInfo(ID,hoppy="打篮球",*Info,**content):
def printStuInfo_1(ID,*Info,**contents):
    print("ID =",ID)
    for info in Info:
        print(info,end=", ")  
    print()
    for k,v in contents.items():
        print("关键字是:{0} 值是:{1}".format(k,v))


print("示例1".center(50,"-"))
printStuInfo_1(10008,"性别男","民族汉",\
    江苏="南京",zhejiang="杭州",hebei="石家庄")


def printStuInfo_2(ID,hobby="打篮球",*Info,**contents):
    print("ID =",ID)
    for info in Info:
        print(info,end=", ")  
    print()
    for k,v in contents.items():
        print("关键字是:{0} 值是:{1}".format(k,v))
    print("业余爱好是:",hobby)

print("示例2".center(50,"-"))
printStuInfo_2(10008,"性别男","民族汉","籍贯浙江",\
    江苏="南京",zhejiang="杭州",hebei="石家庄")
# 注意,这里的业余爱好的参数会有问题

print("示例3".center(50,"-"))
printStuInfo_2(10008,"打篮球","性别男","民族汉","籍贯浙江",\
    江苏="南京",zhejiang="杭州",hebei="石家庄")




def printStuInfo_3(ID,*Info,hobby="打篮球",**contents):
    print("ID =",ID)
    for info in Info:
        print(info,end=", ")  
    print()
    for k,v in contents.items():
        print("关键字是:{0} 值是:{1}".format(k,v))
    print("业余爱好是:",hobby)
print("示例4".center(50,"-"))
printStuInfo_3(10008,"性别男","民族汉","籍贯浙江","打篮球",\
    江苏="南京",zhejiang="杭州",hebei="石家庄")

print("示例5".center(50,"-"))
printStuInfo_3(10008,"性别男","民族汉","籍贯浙江",hobby="打篮球",\
    江苏="南京",zhejiang="杭州",hebei="石家庄")

""" 
# 将关键词参数放在**contents之后会报错
def printStuInfo_4(ID,*Info,**contents,hobby="打篮球"):
    pass 

 """



""" 
# 默认值的参数 放在无默认值的参数前也会报错
# SyntaxError: non-default argument follows default argument
def printStuInfo_5(hobby="打篮球",ID,*Info,**contents):
    pass 

 """




""" 
# **contents放在*Info前面也会报错,
#   File "c:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹\test.py", line 79
#     def printStuInfo_5(**contents,*Info):
#                                   ^
# SyntaxError: invalid syntax
def printStuInfo_5(**contents,*Info):
    pass 

 """


控制台下的输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher' '56328' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\新建文
件夹\test.py'
-----------------------示例1------------------------
ID = 10008
性别男, 民族汉, 
关键字是:江苏 值是:南京
关键字是:zhejiang 值是:杭州
关键字是:hebei 值是:石家庄
-----------------------示例2------------------------
ID = 10008
民族汉, 籍贯浙江, 
关键字是:江苏 值是:南京
关键字是:zhejiang 值是:杭州
关键字是:hebei 值是:石家庄
业余爱好是: 性别男
-----------------------示例3------------------------
ID = 10008
性别男, 民族汉, 籍贯浙江, 
关键字是:江苏 值是:南京
关键字是:zhejiang 值是:杭州
关键字是:hebei 值是:石家庄
业余爱好是: 打篮球
-----------------------示例4------------------------
ID = 10008
性别男, 民族汉, 籍贯浙江, 打篮球, 
关键字是:江苏 值是:南京
关键字是:zhejiang 值是:杭州
关键字是:hebei 值是:石家庄
业余爱好是: 打篮球
-----------------------示例5------------------------
ID = 10008
性别男, 民族汉, 籍贯浙江, 
关键字是:江苏 值是:南京
关键字是:zhejiang 值是:杭州
关键字是:hebei 值是:石家庄
业余爱好是: 打篮球
PS C:\Users\chenxuqi\Desktop\新建文件夹\新建文件夹> 
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页