不定长参数的传递

Posted 忍冬的你

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不定长参数的传递相关的知识,希望对你有一定的参考价值。

 1 def Result(a,*b):
 2     result=0
 3     result+=a
 4     for x in b:
 5         result+=x
 6     return result
 7 
 8 add_result=Result(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39)
 9 # add_result=Result(2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38)
10 print(add_result)
View Code

除此之外,在python的函数中我们传递的参数有些还有特殊的含义,比如def test(a,b,c=8,count=\'china\')

1 # test2()
2 
3 def Result(a,*b,**c):
4    print(a)
5    print(b)
6    print(c)
7 
8 Result(1,3,5,7,11,13,15,17,19,21,23,25,27,31,33,35,37,f=29,t=9,g=39)
View Code

运行结果:

E:\\Python35\\python.exe F:/Exercise/Python/test0731/登陆.py
1
(3, 5, 7, 11, 13, 15, 17, 19, 21, 23, 25, 27, 31, 33, 35, 37)
{\'t\': 9, \'f\': 29, \'g\': 39}

Process finished with exit code 0

 

 

备注:* 和**参数都是可传可不传递的,因此必须把必须要传的放在第一位,这个一定要切记。

以上是关于不定长参数的传递的主要内容,如果未能解决你的问题,请参考以下文章

网络数据传输--编码转换,不定长参数传递,字符串拼接,URLencode编码

网络数据传输--编码转换,不定长参数传递,字符串拼接,URLencode编码

python中函数的不定长参数

嵌入式不定长的任务消息传递组件的实现

2022年最新Python大数据之Python基础参数与管理系统

2022年最新Python大数据之Python基础参数与管理系统