python--變量修改

如果是對全局變量的修改用:global

如果是對閉包中函數的局部變量的修改請用:nonlocal

x = 300
def test1():
	x = 200
	def test2():
		nonlocal x
		print("----1----x=%d" % x)
		x = 100
		print("----2----x=%d" % x)

	return test2

t1 = test1()
t1()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章