2015-01-26 17:41:33 +08:00
|
|
|
#!/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()
|
2015-01-26 17:42:38 +08:00
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
class ClosureInstance:
|
|
|
|
|
def __init__(self, locals=None):
|
|
|
|
|
if locals is None:
|
|
|
|
|
locals = sys._getframe(1).f_locals
|
|
|
|
|
|
|
|
|
|
# Update instance dictionary with callables
|
|
|
|
|
self.__dict__.update((key,value) for key, value in locals.items()
|
|
|
|
|
if callable(value) )
|
|
|
|
|
# Redirect special methods
|
|
|
|
|
def __len__(self):
|
|
|
|
|
return self.__dict__['__len__']()
|
|
|
|
|
|
|
|
|
|
# Example use
|
|
|
|
|
def Stack():
|
|
|
|
|
items = []
|
|
|
|
|
def push(item):
|
|
|
|
|
items.append(item)
|
|
|
|
|
|
|
|
|
|
def pop():
|
|
|
|
|
return items.pop()
|
|
|
|
|
|
|
|
|
|
def __len__():
|
|
|
|
|
return len(items)
|
|
|
|
|
|
|
|
|
|
return ClosureInstance()
|
|
|
|
|
|
|
|
|
|
s = Stack()
|
|
|
|
|
print(s)
|
|
|
|
|
s.push(10)
|
|
|
|
|
s.push(20)
|
|
|
|
|
print(len(s))
|
|
|
|
|
print(s.pop())
|
|
|
|
|
print(s.pop())
|