## 七、Python的函數編程(之一)
----From a high schoolstudent's view to learn Python
關鍵字:
python 高中生學編程 MIT公開課視頻Python函數編程 遞歸 遞歸函數 遞歸調用
一、為什么會引入函數
------------------------
在學習的初期階段,我們寫的程序都比較簡單,功能比較單一,代碼的行數也不會太多;那我們想一想,如果需要處理的應用越來越復雜,我們是不是順著我們的思路,一直將我們的代碼在一個地方不停地寫下去呢?如果這樣下去,會是什么結果呢?
你可以想一想,如果一部長篇小說,沒有章節劃分,甚至連段落都沒有,拿到它進行閱讀,會是怎樣的閱讀感受呢?
其實,我們編寫代碼,一方面實現代碼的功能,另一方面還需要具有較好的可讀性,便于維護、修改、和別人使用;所以,隨著功能變得復雜,我們會將應用進行分割,簡單化、模塊化,就象買回來的組裝玩具一樣,函數(Function)就是這些玩具的各個部件。
我們在學習之初,都用到了print,print其實就是一個函數,只不過它是系統提供的一個函數,他可以被任何人在任何時候使用,來實現它的功能。
下面剪輯的視頻還是來自MIT的公開課視頻,比較詳細的介紹了在編程中為何需要引入函數;
【這里插入視頻】
為什么需要引入函數的視頻
[](http://video.sina.com.cn/api/outPlayRefer.php/vid=116153277/uid=3603736894/pid=346/tid=1/s.swf)
函數的實際應用講解
[](http://video.sina.com.cn/api/outPlayRefer.php/vid=116151476/uid=3603736894/pid=346/tid=1/s.swf)
二、函數的定義
---------------
1. 函數的定義
我們從一個例子開始,這是一個可以計算任意大小的斐波那契數列的函數:
<table cellspacing="0" cellpadding="0" style="border-collapse:collapse"><tbody><tr><td valign="top" style="width:25.9px; height:128.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 3.0px 1.0px 1.0px; border-color:#000000 #429287 #000000 #000000"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">1</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">2</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">3</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">4</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">5</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">6</span></p><p style="margin:0px; font-size:12px; line-height:normal; font-family:Helvetica; min-height:14px"><br/></p></td><td valign="top" style="width:401.0px; height:128.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 1.0px 1.0px 3.0px; border-color:#000000 #000000 #000000 #429287"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">def fib(n):?<wbr>?<wbr><em># write Fibonacci series up ton</em>?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr><em>"""Print a Fibonacci series up ton."""</em>?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> a, b = 0, 1?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> while a <n:?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> printa,?<wbr/></wbr></wbr></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> a, b = b,a+b</wbr></wbr></wbr></wbr></span></p><p style="margin:0px; font-size:12px; line-height:normal; font-family:Helvetica; min-height:14px"><br/></p></td></tr></tbody></table>
在詳細的介紹函數的定義之前,我們先看看這段代碼:
line 1:定義函數
line2:還記得嗎,這是一行注釋,三引號(單引號效果一項)
line3:比較奇特,其實就是賦值語句,對于多個變量需要賦值時,可以按照這樣的方式來寫,看著比較簡潔,按照等號前面的變量順序,逐個將后面的值賦給變量,這里給變量賦的都是整數,其實任意類型都可以,如:
aa, bb, cc, dd = 1, 2.3, 1.3e2,“string”
line4:while循環語句,注意寫這樣的語句,不要忘記”:”
line5:這一句我們應該很熟了,但是注意a后面的”,”;這表示在print變量a之后,不換行,因為是循環,所以在下次執行這個print語句時,print的內容會跟在上一次print的后面,不會換行;但是,如果沒有”,”那每次執行完print都會自動換行
line 6:在line3我們介紹了這是一種賦值的寫法,這一句又深入了,賦值的內容是多個表達式
特別再次強調:注意縮進,本例有兩層縮進
函數的定義:
要素一:關鍵字def
要素二:函數名,和變量名的規則差不多,但是一般好的編程風格是將函數名和它實現的功能變現出來
要素三:函數名后面有一對()
要素四:()中可以含有一個和多個參數,當然也可以沒有參數
要素五:”:”
要素六:函數體必須縮進
2. 函數的調用
函數在定義了之后,我們只有進行調用,函數才能夠執行,其實在使用print語句的時候,我們就是在“調用print函數“。
那如何調用我們在上面給出的示例呢?
...>>>*# Now call the function wejust defined:*?
...fib(2000)?
0 1 1 2 3 5 8 13 21 34 55 89144 233 377 610 987 1597?
fib(2000)就是調用,在下面的一行,就是該函數的執行結果,顯示了2000以下的斐波那契數列;因為我們在printa后面加了”,”,所以數列顯示在一行,否則,這些數將一行一個豎著顯示出來。
3. 關于函數的參數
函數的參數對我們初學者,是比較繞的(個人感覺),其實跟我們數學中的函數有異曲同工之處,數學中的函數如:
y=f(x)
y=f(x1,y1)
y=f(x1, x2, …, xn)
所以,我們可以這么理解:
函數的參數就相當于我們數學中函數的自變量。
函數可以沒有參數,也可以有一個或多個參數。
還有一種特殊情況:
回想一下print的使用,我們可以:
print a
也可以:
print a, b, c, d, e
也就是說,參數的數量是可變的。
以上只是簡單的概念性介紹,python在函數參數方面還有很多特點:
1)?**缺省參數(default argumentvalues)**
<table cellspacing="0" cellpadding="0" style="border-collapse:collapse"><tbody><tr><td valign="top" style="width:25.9px; height:128.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 3.0px 1.0px 1.0px; border-color:#000000 #429287 #000000 #000000"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">1</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">2</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">3</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">4</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">5</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">6</span></p><p style="margin:0px; font-size:12px; line-height:normal; font-family:Helvetica; min-height:14px"><br/></p></td><td valign="top" style="width:401.0px; height:128.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 1.0px 1.0px 3.0px; border-color:#000000 #000000 #000000 #429287"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">deffib(n=2000):?<wbr> ?<wbr><em># writeFibonacci series up to n</em>?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr><em>"""Print a Fibonacci series up ton."""</em>?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> a, b = 0, 1?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> while a <n:?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> printa,?<wbr/></wbr></wbr></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> a, b = b,a+b</wbr></wbr></wbr></wbr></span></p><p style="margin:0px; font-size:12px; line-height:normal; font-family:Helvetica; min-height:14px"><br/></p></td></tr></tbody></table>
這樣定義的函數,我們在調用時,可以直接使用
fib()?
0 1 1 2 3 5 8 13 21 34 55 89144 233 377 610 987 1597?
但是,如果不是缺省參數定義,這樣調用系統會報錯:
~~~
fib()
Traceback (most recent calllast):
File"<stdin>", line 1, in<module>
TypeError: fib() takes exactly1 argument (0 given)
~~~
**2) 關鍵字參數(keywordarguments)**
關鍵字參數是針對函數調用而進行設計的語法規則;
對于函數:
function_ex(arg1, arg2=”arg2”,arg3)
pass#關鍵字,用來占位
我們可以采用以下的幾種方法來調用(我們假設參數的類型為string):
~~~
function_ex(“arg1”)
function_ex(arg1=”arg111”,“arg333”)
function_ex(”arg111”,arg3=“arg333”)
function_ex(arg3=”arg333”,arg1=”arg111”,arg2=”arg222”)
~~~
采用關鍵字參數可以不按照參數列表的實際順序,但是盡量按照順序來寫,否則很容易引起錯誤
4.?**函數的返回值**
還是拿我們數學的函數來進行說明:
對于函數:
y=f(x)
y=f(x1,y1)
y=f(x1, x2, …, xn)
如果我們給定自變量值,肯定有一個y值對應,我們可以類似的認為,y就是函數的返回值。
將上面的fib函數稍作修改:
<table cellspacing="0" cellpadding="0" style="border-collapse:collapse"><tbody><tr><td valign="top" style="width:25.9px; height:147.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 3.0px 1.0px 1.0px; border-color:#000000 #429287 #000000 #000000"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">1</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">2</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">3</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">4</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">5</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">6</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">7</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">8</span></p></td><td valign="top" style="width:401.0px; height:147.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 1.0px 1.0px 3.0px; border-color:#000000 #000000 #000000 #429287"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">def fib2(n): <em># returnFibonacci series up to n</em>?<wbr/></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr><em>"""Return a list containing the Fibonacciseries up to n."""</em>?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> result = []?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> a, b = 0, 1?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> while a <n:?<wbr/></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>result.append(a)?<wbr> ?<wbr><em># seebelow</em>?<wbr/></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> a, b = b,a+b?<wbr/></wbr></wbr></wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> return result</wbr></wbr></span></p></td></tr></tbody></table>
修改之處有:
line1:我們重新取了個名,現在函數名為fib2
line3:新增加了一個變量result,還記得嗎,[]表示list類型,我們給result初始化成一個空得list,這一句實現了兩個目的:一是隱含的說明了result是一個list類型的變量,二是對變量進行了初始化
line6:這句會比較難懂,list類型在前面的博客中已經有介紹,它包含有有很多的“方法(method)“,來實現各種不同的功能,如:append用來添加一個元素到list,pop可以將list的最后一個元素彈出等…,注意這種調用的寫法。result.append(a)等同于result= result + [a]
line7:返回result
對于返回值,還需要強調:
要素一:
函數可以沒有return語句,但在沒有return時,返回值是None,這也是一個保留關鍵字;
~~~
fib(0)?
printfib(0)?
None
~~~
要素二:
return語句可以沒有值,這種情況,也返回None
要素三:
正常情況,return value
下面是一段視頻講解
插入視頻
[](http://video.sina.com.cn/api/outPlayRefer.php/vid=116151408/uid=3603736894/pid=346/tid=1/s.swf)
三、變量的作用域
---------------------
我們在程序中會定義很多的變量,在引入了函數之后,變量的作用域(或者說變量的可使用范圍)就需要仔細的考慮了。
在函數的外面定義的變量,在函數里可以使用嗎?
在函數里定義的變量,在函數體之外還能夠使用嗎?
這些都是我們在這一節討論的問題。
1、局部變量
簡單的說,在函數體之內定義的變量,它的作用域就被限定在函數體內,如果在函數體外使用,就會報錯
def func_test01():
... ?? a1=10
... ?? print a1
...?
func_test01()
10
print a1
Traceback (most recent calllast):
File"<stdin>", line 1, in<module>
NameError: name 'a1' is notdefined
2、全局變量
如上述,反之就是全局變量,看如下示例:
~~~
a1=1
def test_func():
... ?? print a1
... ?? print a2
...?
a2=2
test_func()
1
2
~~~
就算a2定義在函數的后面,在函數中也是可以使用的
3、強制為全局變量
如果在函數體內聲明一個與全局變量一樣名字的變量時,這時候,全局變量將被覆蓋,如同聲明了一個名字一樣的局部變量。
<table cellspacing="0" cellpadding="0" style="border-collapse:collapse"><tbody><tr><td valign="top" style="width:25.9px; height:166.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 3.0px 1.0px 1.0px; border-color:#000000 #429287 #000000 #000000"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">1</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">2</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">3</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">4</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">5</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">6</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">7</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">8</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">9</span></p></td><td valign="top" style="width:401.0px; height:166.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 1.0px 1.0px 3.0px; border-color:#000000 #000000 #000000 #429287"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">def foo():</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> print "\ncalling foo()..."</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> bar = 200</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> print "in foo(), bar is", bar</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144); min-height:14px"><br/></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">bar = 100</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">print "in __main__, bar is",bar</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">foo()</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">print "\nin __main__, bar is(still)", bar</span></p></td></tr></tbody></table>
得出的輸出結果如下:
~~~
in __main__, bar is100
calling foo()...
in foo(), bar is 200
in __main__, bar is (still)100
~~~
如何解決這種問題?這就需要使用global來強制將變量設為全局變量,改動如下:
<table cellspacing="0" cellpadding="0" style="border-collapse:collapse"><tbody><tr><td valign="top" style="width:25.9px; height:185.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 3.0px 1.0px 1.0px; border-color:#000000 #429287 #000000 #000000"><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">1</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">2</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">3</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">4</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">5</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">6</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">7</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">8</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">9</span></p><p style="margin:0px 0px 5px; text-align:right; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">10</span></p></td><td valign="top" style="width:401.0px; height:185.0px; background-color:#ffffff; border-style:solid; border-width:1.0px 1.0px 1.0px 3.0px; border-color:#000000 #000000 #000000 #429287"><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">def foo():</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> global bar</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> print "\ncalling foo()..."</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> bar = 200</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">?<wbr>?<wbr> print "in foo(), bar is", bar</wbr></wbr></span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144); min-height:14px"><br/></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">bar = 100</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">print "in __main__, bar is",bar</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">foo()</span></p><p style="margin:0px 0px 5px; font-size:12px; line-height:normal; font-family:Arial; color:rgb(1,24,144)"><span style="letter-spacing:0.0px">print "\nin __main__, bar is(still)", bar</span></p></td></tr></tbody></table>
得出的輸出結果如下:
~~~
in __main__, bar is100
calling foo()...
in foo(), bar is 200
in __main__, bar is (still)200
~~~
這樣的話,在調用函數之后,bar的值變被更改了。
我的更多文章:
- Python程序調試的一些體會(2013-10-06 22:57:35)
- 十四、Python編程計算24點(之二)(2013-10-03 22:18:28)
- 十三、Python編程計算24點(之一)
(2013-10-02 22:15:46)
- 十二、Python簡單數據結構應用(之二)(2013-10-02 22:10:41)
- 十一、Python簡單數據結構應用(之一)(2013-09-23 23:31:49)
- 十、Python編程解決組合問題(之二)
(2013-09-21 23:37:27)
- 九、Python編程解決組合問題(之一)(2013-09-21 23:32:54)
- 八、Python的函數編程(之二)
(2013-09-20 23:09:39)
- 六、Python的程序流程(2013-09-19 16:53:58)
- 高中生如何學編程(2013-09-02 19:26:01)