Ray's playground

 

Python虚拟机框架(Python 源码剖析第八章)

frame
 1 >>> import sys
 2 >>> value = 3
 3 >>> def g():
 4     frame = sys._getframe()
 5     print 'current function is  : ', frame.f_code.co_name
 6     caller = frame.f_back
 7     print 'caller function is : ', caller.f_code.co_name
 8     print "caller's local namespace: ", caller.f_locals
 9     print "caller's global namespace: ", caller.f_globals.keys()
10 
11     
12 >>> def f():
13     a = 1
14     b = 2
15     g()
16 
17     
18 >>> def show():
19     f()
20 
21     
22 >>> show()
23 current function is  :  g
24 caller function is :  f
25 caller's local namespace:  {'a': 1, 'b': 2}
26 caller's global namespace:  ['g''f''__builtins__''show''value''__package__''sys''__name__''__doc__']

 

 

error
 1 >>> a = 1
 2 >>> def g():
 3     print a
 4 
 5     
 6 >>> def f():
 7     print a
 8     a = 2
 9     print a
10 
11     
12 >>> g()
13 1
14 >>> f()
15 
16 Traceback (most recent call last):
17   File "<pyshell#11>", line 1in <module>
18     f()
19   File "<pyshell#9>", line 2in f
20     print a
21 UnboundLocalError: local variable 'a' referenced before assignment 

 

posted on 2010-11-17 21:34  Ray Z  阅读(334)  评论(0编辑  收藏  举报

导航