python 学习笔记——python globals()
2015-01-09 20:08
197 查看
globals() : 将python的全局变量包括类,包名等全局属性封装在一个字典里面,key是属性名,value是属性值
另外还有locals() 对应着局部变量,不再详述
# 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() 对应着局部变量,不再详述
相关文章推荐
- Python两个内置函数 locals 和globals(学习笔记)
- python学习笔记012——locals与globals
- Python两个内置函数——locals 和globals (学习笔记)
- Python两个内置函数——locals 和globals (学习笔记)
- Python两个内置函数——locals 和globals (学习笔记)
- Python两个内置函数——locals 和globals (学习笔记)
- Python两个内置函数 locals 和globals(学习笔记)
- python学习笔记(二)
- Python学习笔记(二)
- Python 学习笔记 - 4.if 表达式
- python文件读写实例学习笔记
- Python 学习笔记(1)
- Python学习笔记(1)
- python(异常处理机制,学习笔记摘要)
- python学习笔记(1)
- python学习笔记,整形,字符串
- Python学习笔记1
- if __name__ (python学习笔记一,注意前面是双下划线)
- python基础学习笔记分享版(1)
- python学习笔记(2)