<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[樱花浪子'S BLOG]]></title> 
<link>http://hacklu.net/blog/index.php</link> 
<description><![CDATA[叶的飘落是风的追求，还是树的不挽留。]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[樱花浪子'S BLOG]]></copyright>
<item>
<link>http://hacklu.net/blog/post//</link>
<title><![CDATA[python3学习笔记5]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 10 Jun 2009 04:06:51 +0000</pubDate> 
<guid>http://hacklu.net/blog/post//</guid> 
<description>
<![CDATA[ 
	<br/>5.Python:函数<br/><br/>函数是可重用的程序块，给程序块起个名字就可以再其他程序中引用改名字，这就是函数调用。已经用过的函数如len和range。<br/><br/>函数使用def关键字来定义，函数名后跟括号，括号中包含变量名，括号后跟冒号。然后是函数代码块。<br/><br/>def sayHello():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Hello world')<br/><br/>sayHello()<br/><br/>sayHello()<br/><br/>运行输出：<br/><br/>>>><br/><br/>Hello world<br/><br/>Hello world<br/>函数参数<br/><br/>包含在括号中，用逗号分开，调用运行时赋值。<br/><br/>def printMax(a,b):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if a>b:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a,'is maxium')<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;elif a==b:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a,'is equal to',b)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(b,'is maxium')<br/><br/>printMax(3,4)<br/><br/>x=5<br/><br/>y=7<br/><br/>printMax(x,y)<br/><br/>运行输出：<br/><br/>>>><br/><br/>4 is maxium<br/><br/>7 is maxium<br/>局部变量<br/><br/>局部变量是在函数内部声明的变量，生存域在函数内，可以与函数外的变量同名。<br/><br/>x=50<br/><br/>def func(x):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('x is' ,x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=2<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('changed local x to',x)<br/><br/>func(x)<br/><br/>print('x is still',x)<br/><br/>运行输出：<br/><br/>>>><br/><br/>x is 50<br/><br/>changed local x to 2<br/><br/>x is still 50<br/>使用全局申明<br/><br/>如果你想在程序的最外层定义变量，你必须告诉python这个变量不是局部的，而是全局的，这可以用global语句来完成。不使用global语句是无法对程序外部定义的变量赋值的。如果在程序内部没有同名变量，那么你可以直接赋值，但是这样很不清楚。<br/><br/>x=50<br/><br/>def func():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;global x<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('x is',x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=2<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('changed global x to',x)<br/><br/>func()<br/><br/>print('Value of x is',x)<br/><br/>运行输出：<br/><br/>>>><br/><br/>x is 50<br/><br/>changed global x to 2<br/><br/>Value of x is 2<br/>使用nonlocal语句<br/><br/>在局部和全局之间还有一种域，叫做非局部域nonlocal，当在函数中定义函数时，就会出现非局部域。<br/><br/>def func_outer():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=2<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('x is',x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;def func_inner():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nonlocal x<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x=5<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;func_inner()<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Changed local x to',x)<br/><br/>func_outer()<br/><br/>运行输出：<br/><br/>>>><br/><br/>x is 2<br/><br/>Changed local x to 5<br/>缺省参数值<br/><br/>某些函数的参数需要设置成可选的，当用户没有指定该参数值时，函数中使用此参数的默认值。设置默认值的方法是在函数声明时该参数后跟“=”和默认值，注意默认值应该是常量，或者说是不可变值。<br/><br/>def say(message,times=1):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(message*times)<br/><br/>say('Hello')<br/><br/>say('World',5)<br/><br/>运行输出：<br/><br/>>>><br/><br/>Hello<br/><br/>WorldWorldWorldWorldWorld<br/><br/>注意：具有默认值的参数应该放在没有默认值参数的后面。<br/>关键字参数<br/><br/>指定参数值时可以使用参数名，也可以按照参数在函数定义中的位置。这样做有两个好处，一是不用记参数的顺序了，二是只需要给必须赋值的参数赋值。<br/><br/>def func(a,b=5,c=10):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('a is',a,'and b is',b,'anc c is',c)<br/><br/>func(3,7)<br/><br/>func(25,c=24)<br/><br/>func(c=50,a=100)<br/><br/>运行输出：<br/><br/>a is 3 and b is 7 anc c is 10<br/><br/>a is 25 and b is 5 anc c is 24<br/><br/>a is 100 and b is 5 anc c is 50<br/>可变参数函数<br/><br/>有时候你可能需要定义一个具有可变参数个数的函数，使用‘*’可以达到效果。<br/><br/>def total(initial=5,*numbers,**keywords):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;count=initial<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;for number in numbers:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count +=number<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;for key in keywords:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count += keywords[key]<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;return count<br/><br/>print(total(10,1,2,3,vegetables=50,fruits=100))<br/><br/>运行输出：<br/><br/>>>><br/><br/>166<br/><br/>当我们声明一个带星号的参数如*param，那么从这一个参数到结束的参数被收集成一个list，称为param。同样，当我们声明一个带双星号的参数如**param，那么从这个参数到结束的参数被收集成一个字典，称为param。<br/><br/>在后面的章节会讲list和dictionary的知识。<br/>只能用关键字赋值的参数<br/><br/>如果你想指定某个参数只能通过关键字赋值，而不能靠位置来赋值，你可以声明这个参数为带星号的参数。<br/><br/>def total(initial=5,*numbers,vegetables):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;count=initial<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;for number in numbers:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count +=number<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;count +=vegetables<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;return count<br/><br/>print(total(10,1,2,3,vegetables=50))<br/><br/>print(total(10,1,2,3))<br/><br/>运行输出：<br/><br/>>>><br/><br/>66<br/><br/>Traceback (most recent call last):<br/><br/>File "C:/Python30/mywork/func1.py", line 8, in <module><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(total(10,1,2,3))<br/><br/>TypeError: total() needs keyword-only argument vegetables<br/><br/>注意：Keyword-only参数后面的参数必须指定默认值。<br/>return 语句<br/><br/>return语句用来从函数中返回，比如跳出函数，返回值是可选的。<br/><br/>def maxium(x,y):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if x>y:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return y<br/><br/>print(maxium(2,3))<br/><br/>运行输出：<br/><br/>>>><br/><br/>3<br/><br/>return不带返回值相当于return None。每个函数最后都隐式的包含return None语句，除非你写了自己的return语句。<br/><br/>def someFunction():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;pass<br/><br/>print(someFunction())<br/><br/>运行输出：<br/><br/>>>><br/><br/>None<br/><br/>pass表示空的代码块。内建函数max能够完成找到最大的功能。<br/>文档字符串<br/><br/>Python有一个极好的特性——文档字符串DocStrings。<br/><br/>def printMax(x,y):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;'''Prints the maxium of two numbers.<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;The two values must be integers.'''<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=int(x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;y=int(y)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if x>y:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(x,'is maxium')<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(y,'is maxium')<br/><br/>printMax(3,5)<br/><br/>print(printMax.__doc__)<br/><br/>运行输出：<br/><br/>>>><br/><br/>5 is maxium<br/><br/>Prints the maxium of two numbers.<br/><br/>The two values must be integers.<br/><br/>函数逻辑行的第一行的字符串就是该函数的文档字符串，模块和类中都有文档字符串的应用。文档字符串的传统是第一行首字母大写并以点结束，第二行为空，第三行为详细介绍。使用__doc__属性可以得到文档字符串。Help函数就是读取函数的文档字符串并显示出来。我强烈建议大家写程序时遵守文档字符串传统。<br/>注解<br/><br/>函数的另一高级特性是注解，在这里可以很好的了解参数和返回值。如需详细了解注解请参考Python Enhancement Proposal No. 3107 (<a href="http://www.python.org/dev/" target="_blank">http://www.python.org/dev/</a> peps/pep-3107/ )
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post//#blogcomment</link>
<title><![CDATA[[评论] python3学习笔记5]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://hacklu.net/blog/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>