FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
learn-python/closure.py at master · cscis/learn-python · GitHub
cscis
/
learn-python
Public
forked from
tanteng/learn-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
learn-python
/
closure.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (30 loc) · 856 Bytes
Breadcrumbs
learn-python
/
closure.py
Copy path
File metadata and controls
35 lines (30 loc) · 856 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def
hellocounter
(
name
):
count
=
0
#PYTHON 2.x 中,要写count=[0]
def
counter
():
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
return
adder
p
=
make_adder
(
23
)
q
=
make_adder
(
44
)
print
(
p
(
100
))
print
(
q
(
100
))
Back
|
FazBrowse Home
|
New Git URL