Does any language or debug tool have a way to print out the scope chain for examination? Thanks.
views:
31answers:
1
+2
A:
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.
bobince
2010-05-03 23:55:39