Does any language (or debugging tool) have a build in function or method to print out the scope chains?
Does any language or debug tool have a way to 开发者_JAVA百科print out the scope chain for examination, so as to look at the different situations of what a scope chain contains?
Firebug does for JavaScript. On the ‘Watch’ tab of the ‘Script’ debugger you can open up the scope chain list a look at each parent scope.
Python can read locals from a parent scope in the language itself if you grab a code object, but the way it handles nested scopes means that only the scoped variables that are actually used are bound:
>>> def a():
... def b():
... print v1
... v1= 1
... v2= 2
... return b
>>> f= a()
>>> f.func_code.co_freevars
('v1',)
>>> f.func_closure
(<cell at 0x7fb601274da8: int object at ...>,)
>>> f.func_closure[0].cell_contents
1
Though both v1
and v2
are defined in the parent scope, only v1
is actually closed over.
精彩评论