API文档:
- globals()
- Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).
描述:
返回一个字典的。该字典中存储则全局的变量值。全局主要是当前的模块,包括导入到本地的模块例如:from sgmllib import SGMLParser那么SGMLParser中定义的变量就被添加到了globals的字典中。
例子:
#! /usr/bin/env python
#coding=utf-8
aa='ccc'
from sgmllib import SGMLParser
class TestC:
b = 'bb'
def f(self,x):
print len(globals())
def fun(param):
x = 0
print len(globals())
fun('cc')
t = TestC()
t.f('g')
输出:
9
10