後陣列值。如果我這樣寫:記住函數調用
c = []
def cf(n):
c = range (5)
print c
if any((i>3) for i in c) is True:
print 'hello'
cf(1)
print c
然後我得到:
[1, 2, 3, 4]
hello
[]
我真的很新的節目,所以請真正簡單的解釋,但怎麼辦我停止Python忘記函數結束後什麼是c
?我認爲我可以通過在函數前定義c
來修復它,但顯然c
與僅爲函數循環創建的不同。
在我的例子,我可以明明只是寫:
c = range (5)
def cf(n)
但我試圖寫的程序更像是這樣的:
b = [blah]
c = []
def cf(n):
c = [transformation of b]
if (blah) is True:
'loop' cf
else:
cf(1)
g = [transformation of c that produces errors if c is empty or if c = b]
所以我不能確定外c
功能。
放'全球C'在你的函數的頂部,它會使用在你f的頂層中定義的版本ile,而不是在函數內部創建一個新的變量。 – 2014-12-04 23:36:31
'全球c'在'cf'中使用此 – Marcin 2014-12-04 23:37:17
真棒,歡呼! – Patrik333 2014-12-04 23:43:21