python3-可变参数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3-可变参数相关的知识,希望对你有一定的参考价值。
def personinfo(name,**info):
print(name)
print(info)
for key in info:
print(key)
print(info[key])
personinfo("zhangsang",age=25,sex="man")
personinfo("lisi",age=29,sex="woman")
zhangsang
{‘age‘: 25, ‘sex‘: ‘man‘}
age
25
sex
man
lisi
{‘age‘: 29, ‘sex‘: ‘woman‘}
age
29
sex
woman
def personinfo(name,**info):
print(name)
print(info)
for key in info:
print(key)
print(info[key])
students={"age":32,"sex":"man"}
personinfo("zhangsang",age=25,sex="man")
personinfo("lisi",age=29,sex="woman")
personinfo("wangwu",**students)
zhangsang
{‘age‘: 25, ‘sex‘: ‘man‘}
age
25
sex
man
lisi
{‘age‘: 29, ‘sex‘: ‘woman‘}
age
29
sex
woman
wangwu
{‘age‘: 32, ‘sex‘: ‘man‘}
age
32
sex
man
def personinfo(name,*info):
print(name)
print(info)
for name,value in info:
print(name),
print(value)
students=[("age",32),("sex","man")]
personinfo("zhangsang",("age",25),("sex","man"))
personinfo("wangwu",*students)
zhangsang
((‘age‘, 25), (‘sex‘, ‘man‘))
age
25
sex
man
wangwu
((‘age‘, 32), (‘sex‘, ‘man‘))
age
32
sex
man
以上是关于python3-可变参数的主要内容,如果未能解决你的问题,请参考以下文章
Python3----函数的参数是可变参数,将传进来的参数转成列表