應該唔係話你啦,上面嗰兩個syntax error都敢寫出嚟,柒到
不過巴打留意吓啲convention,每一隻language都有自己嘅 casing convention
Python就除咗class name係PascalCase/TitleCase之外,其餘基本上全snake_case
res = 0
def A():
res = 1+1
def B():
print (res)
A()
B()
一開波declare咗先同係個function入面用global有咩分別?其實如果你想獨立拆開function a 既結果同其中一part 讀數其實解決你個問題好易,但重點係點解你想咁做用global既方法得左啦
假設你function A既計算好複雜,你點保證function B read variable AB果時已經計完?如果個variable會update, 仲要考慮下Thread safe既問題
正如其他人答,最正路係喺function A 尾return result:
def A(): res = 1+1 #sample return res def B(): print (A())
唔建議既方法係declare global:
def A(): global res res = 1+1 def B(): print (res)
感謝大大佬
你應該寫多個function 出黎return AB
下下用global 解到一時問題但之後好多手尾
call咗A()之後就一樣,咁點解要用global?
新手求教
即係咁:
res = 0 def A(): res = 1+1 def B(): print (res) A() B()
res = 0
def A():
res = 1+1
print(res) // 2
def B():
print (res) // 0
A()
B()
dir(int)
睇吓就會知其實所有嘢都係object)def a() :
a = 1
return a + 1
res = a()
print('res') // 2