python的参数
2015-11-18 17:00
706 查看
在python的方法中,默认参数通常放在非默认参数的后面:
位置参数:参数的值由对应的位置决定
关键字参数:参数的值由关键字信息决定
接下来看看一类可以输入未知参数个数的方法参数:list param and directonary param
放在带星的参数后面的参数(如果不带*)是关键字参数(不能是位置决定值)
下面代码编译不过:
def out(massege,times=1): print massege*times out('hello ') out('hello ',5) >>> hello hello hello hello hello hello >>>
位置参数:参数的值由对应的位置决定
关键字参数:参数的值由关键字信息决定
def out(a,b,c): print 'a is ',a,' b is ',b,' c is ',c out(1,2,3) out(a=2,b=3,c=1) out(1,c=2,b=3) >>> a is 1 b is 2 c is 3 a is 2 b is 3 c is 1 a is 1 b is 3 c is 2 >>>方法体内的局部变量能覆盖方法外的同名变量的值,使用global statement可以直接使用方法外的变量:
print 'local test:' x=23 def func1(x): print x x=x+1 print x func1(x) print x print 'global test:' x=23 def func2(): global x print x x=x+1 print x func2() print x >>> local test: 23 24 23 global test: 23 24 24 >>>
接下来看看一类可以输入未知参数个数的方法参数:list param and directonary param
def func(intial=0,*a,**m): sum=intial for i in a: sum=sum+i for k in m: sum=sum+m[k] return sum print func(1,2,3,tomato=12,basketball=20); >>> 38 >>>
放在带星的参数后面的参数(如果不带*)是关键字参数(不能是位置决定值)
下面代码编译不过:
def func1(a=0,*b,c): sum=a for i in b: sum=sum+i print sum+c func1(1,2,3,100)
相关文章推荐
- 使用Python 2.7实现的垃圾短信识别器
- python用requests请求百度接口报“SSL: CERTIFICATE_VERIFY_FAILED”
- Python学习指南
- python3输出unicode
- Python中list的实现
- Python处理Json报文
- 【python】class之子类
- python 抽象类、抽象方法的实现
- 安装python MySQLdb报错:pymemcompat.h:10:20: 致命错误的解决
- scikit学习心得——Isotonic Regression
- Python编写微信打飞机小游戏(十二)
- Difference between Range and Xrange in Python
- 折腾Ipython
- 第一个Python程序——博客自动访问脚本
- Python基础学习-爬虫小试2
- python插件
- Python学习小记
- 让cherrypy监听系统内所有活动网卡传入的请求
- python 远程操作ubuntu
- Python 创建函数和代码重用