globals() : 将python的全局变量包括类,包名等全局属性封装在一个字典里面,key是属性名,value是属性值
# coding:utf-8
__author__ = 'taohao'
a = set()
class test1:
def __init__(self):
pass
def a1(self):
pass
def b1(self):
pass
class test2:
def __init__(self):
pass
@classmethod # 注意这里需要用用装饰器装饰一下,不然在外面用globals访问时会报错
def a2(self):
print "test2-------a2"
def b2(self):
pass
g = globals()
print type(g['test2']) #输出 <type 'classobj'>
g['test2'].a2() # 如果a2()函数不用装饰器装饰则会报错:TypeError: unbound method a2() must be called with test2 instance as f# irst argument (got nothing instead)
print g
</pre><pre name="code" class="python">
输出:
{'a': set([]), 'test1': <class __main__.test1 at 0x7fd4a2ea5870>, 'test2': <class __main__.test2 at 0x7fd4a2ea58d8>, 'g': {...}, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'globals.py', '__author__': 'taohao', '__name__': '__main__', '__package__': None, '__doc__': None}
另外还有locals() 对应着局部变量,不再详述