iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 19
0
Software Development

從0開始學習程式-Python系列 第 20

[Day23]OMG!OMG! 跨檔案的global variable不受控!


到現在我還是需要很多咖啡才對QQ
今天我們來講解一下跨檔案之間的全域變數操作吧!

跨檔案的全域變數操作有不一樣?

這是外部建立的finction檔案op.py:

#op.py
count=1
def add():
	global count
	count+=1
	print(count)
	return 
if __name__=='__main__':
	add()

這是主程式檔案main.py:

#main.py
from op import add,count
global count
add()
print(count)

來執行看看結果:

>>> python main.py
2
1


腫麼會長這樣?此時你會發現,我們想要在main.py得到的count應該也是2,卻發現是1?!

那我們來看看程式是怎麼運作的!

為了方便,我們會使用大量print()來看程式是怎麼跑的!

#op.py
count=1
print('In op.py, the 1st time meet count assigning {}'.format(count))
def add():
	global count
	print('In add(), the 1st time meet count assigning {}'.format(count))
	count+=1
	print('In add(), the 2nd time meet count assigning {}'.format(count))
	return 

print('In op.py, the 2nd time meet count assigning {}'.format(count))

if __name__=='__main__':
	print('In controlling, 1st time meet count assigning {}'.format(count))
	add()
	print('In controlling, 2nd time meet count assigning {}'.format(count))
#main.py
from op import add,count
global count
print('In main.py, 1st time meet count assigning {}'.format(count))
add()
print('In main.py, 2nd time meet count assigning {}'.format(count))
>>> python op.py
In op.py, the 1st time meet count assigning 1
In op.py, the 2nd time meet count assigning 1
In controlling, 1st time meet count assigning 1
In add(), the 1st time meet count assigning 1
In add(), the 2nd time meet count assigning 2
In controlling, 2nd time meet count assigning 2

>>> python main.py
In op.py, the 1st time meet count assigning 1
In op.py, the 2nd time meet count assigning 1
In main.py, 1st time meet count assigning 1
In add(), the 1st time meet count assigning 1
In add(), the 2nd time meet count assigning 2
In main.py, 2nd time meet count assigning 1

先針對co.py結果說明

  1. 先來看單純跑co.py的結果,可以知道一開始進行時,會從第一行開始,因此在遇到def add():時,若function是可work的,並不會挑出錯誤訊息,只會先被置放暫時存取待使用,當然此時就會印出In co.py...的結果,此時count依然是1。
  2. 當進到if...這一段控制碼的時候,只要吻合條件,開始進入其下面操作。因此,首先我們先看這時候,在if...階層下第一次遇到的count仍然是1,表示這時候count還沒經過任何操作。
  3. 接著我們call add(),在add()運作時,第一次遇到的count依然是1
  4. 經過count+=1後,第二次印出count變成2了,這時候add()操作完整結束
  5. 回到if...進行下一行,此時透過印出經過add()操作後count是否有改變,結果發現這時候的印出也變為2,也就代表這時候全域變數count已經被改變為2了!

main.py結果說明

那我們來看看另外一組的操作:

  1. 來看一下main.py的結果,可知from co import add,count進行時,會如同上面執行co.py的步驟1一樣會的執行概念
  2. 當進到if...這一段控制碼的時候,因為條件不吻合,不會執行其階層下的操作
  3. 告訴程式count是一個全域變數,接著來到第3行,會print第一次遇到的count是1,因為還沒開始任何的操作
  4. 接著開始使用add()經過count+=1後,第二次印出count變成2了,這時候add()操作完整結束。這一段的結果與前面執行co.py的結果相同。
  5. 最大的不一樣就在這裡了,理論經過add()操作後count已經變成2,要和執行co.py的結果相同才對,但在主程式第二次print(count)的結果卻是1
    切記!這時候執行add()沒有return任何值;再者,即使return count,在main.py需要變數被賦予這個運算結果,即如果想要更改count,則應該改寫成count=add(),接下來第二次print(count)才會是2!

簡言之,若沒有去接執行add()後的count,main.py裡面接受的count就是一開始步驟1所接到的count=1,並不會有改變

其他方法可以完成類似執行co.py的結果?

我們需要透過三個檔案:

  1. variable.py用來儲存變數
#variable.py
def var(): 
	global count
	count = 1
  1. op.py用來操作variable.py裡面的變數
#op.py
import variable 
def add(): 
	variable.count += 1
  1. main.py用來執行全部的操作
#main.py
import variable 
import op

if __name__ == "__main__": 
	variable.var() 
	print(variable.count) # print the initial value 
	op.add() 
	print(variable.count ) # print the value after modified within co.py

執行的結果如下:

>>> python main.py
1
2

這樣你搞懂了嗎?
今天就先到這裡吧!


上一篇
[Day22] I want it that way! global variable就是要這樣用!
下一篇
[Day24]一起來做飲料點餐系統吧!
系列文
從0開始學習程式-Python32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言