您的位置:首页 > 编程语言 > Python开发

python中可变参数个数参数

2013-04-13 21:57 330 查看

args

Table of Contents

1 python中可变参数
1.1 以tuple类型传递
1.2 以dict类型传递

1 python中可变参数

1.1 以tuple类型传递

def argTest(*args):
print(args, type(args))
for i in args:
print(i)

argTest(1,2,3)
argTest(1, "Hello")
=> (1, 2, 3) <class 'tuple'>
1
2
3
(1, 'Hello') <class 'tuple'>
1
Hello


1.2 以dict类型传递

def keyArgTest(**args):
print(args, type(args))
for k,val in args.items():
print(k,val)

keyArgTest(k1=1,k2='123',k3=12.4)
=> {'k1': 1, 'k3': 12.4, 'k2': '123'} <class 'dict'>
k1 1
k3 12.4
k2 123


Date: 2013-04-13 22:01:37 中国标准时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python