32 lines
447 B
Python
32 lines
447 B
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- encoding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
Topic: 闭包访问函数内部变量
|
||
|
|
Desc :
|
||
|
|
"""
|
||
|
|
|
||
|
|
|
||
|
|
def sample():
|
||
|
|
n = 0
|
||
|
|
# Closure function
|
||
|
|
def func():
|
||
|
|
print('n=', n)
|
||
|
|
|
||
|
|
# Accessor methods for n
|
||
|
|
def get_n():
|
||
|
|
return n
|
||
|
|
|
||
|
|
def set_n(value):
|
||
|
|
nonlocal n
|
||
|
|
n = value
|
||
|
|
|
||
|
|
# Attach as function attributes
|
||
|
|
func.get_n = get_n
|
||
|
|
func.set_n = set_n
|
||
|
|
return func
|
||
|
|
|
||
|
|
f = sample()
|
||
|
|
f()
|
||
|
|
f.set_n(10)
|
||
|
|
f()
|