From wikipedia
I need to access outer functions variables in a similar manner 开发者_JAVA技巧as using the 'nonlocal' keyword from python 3.x. Is there some way to do that in python 2.6? (Not necessarily using the nonlocal keyword)
I always use helper objects in that case:
def outerFunction():
class Helper:
val = None
helper = Helper()
def innerFunction():
helper.val = "some value"
This also comes in handy when you start a new thread that should write a value to the outer function scope. In that case, helper
would be passed as an argument to innerFunction
(the thread's function).
精彩评论