Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions 19 closure.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
def hellocounter (name):
count=[0]
count=0 #PYTHON 2.x 中,要写count=[0]
def counter():
count[0]+=1
print('Hello,',name,',',count[0],' access!')
nonlocal count #PYTHON 2.x 中,此行和下一行要换成count[0]+=1
count+=1
print('Hello,',name,',',count,' access!')#PYTHON 2.x 中请自觉换成str(count[0])
return counter

hello = hellocounter('ma6174')
hello()
hello()
hello()

'''
执行结果
>>> hello()
Hello, ma6174 , 1 access!
>>> hello()
Hello, ma6174 , 2 access!
>>> hello()
Hello, ma6174 , 3 access!
'''
##为什么PYTHON 2.x中不直接写count而用list?这是python2的一个bug,如果用count话,会报这样一个错误:
##UnboundLocalError: local variable 'count' referenced before assignment.

def make_adder(addend):
def adder(augend):
return augend + addend
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.