Non-Local Assignment
def make_withdraw(balance):
"Return a withdraw function with a starting balance."
def withdraw(amount):
nonlocal balance # declare the name "balance" nonlocal
if amount > balance:
return "Insufficient funds"
balance -= amount
return balance
return withdraw
withdraw = make_withdraw(100)
withdraw(25)
withdraw(25)
wtihdraw(60)Non-Local 细节
Python particulars(细节)
其他可选的方法
突变会导致 函数的引用透明性(referential transparency) 丢失
Last updated