当前位置: 首页 > 图文教程 > 脚本技术 > Python > 打印出python 当前全局变量和入口参数的所有属性

Python
wxPython 入门教程
Python日期操作学习笔记
Python函数学习笔记
Python转码问题的解决方法
python sqlobject(mysql)中文乱码解决方法
Python 连连看连接算法
Python类的基础入门知识
Python GAE、Django导出Excel的方法
python 参数列表中的self 显式不等于冗余
下载糗事百科的内容_python版
pymssql ntext字段调用问题解决方法
Python 面向对象 成员的访问约束
python 测试实现方法
python 数据加密代码
python zip文件 压缩
python 文件与目录操作
python3.0 字典key排序
Python 学习笔记
Python Mysql数据库操作 Perl操作Mysql数据库
Python MD5文件生成码

Python 中的 打印出python 当前全局变量和入口参数的所有属性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-11   浏览: 174 ::
收藏到网摘: n/a

打印出python 当前全局变量和入口参数的所有属性的实现代码。 def cndebug(obj=False):
"""
Author : Nemon
Update : 2009.7.1
TO use : cndebug(obj) or cndebug() or MyObject.debug=cndebug
License: GPL
"""
print('='*80)
print('='*30 + ' GLOBAL VARIABLES ' +'='*30)
print('='*80)
g=globals()
for x,y in g.iteritems():
if x[:1]!='_':
print ( x + ' := '+ str(type(y)))
print ( y)
print ( '')
if obj:
print('='*80)
print('='*30 + ' LOCAL VARIABLES ' +'='*30)
print('='*80)
for o in dir(obj):
#if o[:1]!='_':
print (o + ' := ' + str(type(getattr(obj,o))))
print ( getattr(obj,o))
print ( '')
print('='*80)
o=raw_input('PRESS <ENTER> TO RESUME...')
del x,y,o

简单用法:
1)打印出python 当前全局变量
cndebug()#
2)打印出当前全局变量和myobj的所有属性
myobj={}
cndebug(myobj)
扩展用法——当作类方法,打印实例的成员
>>> class MyObj():
... debug=cndebug
...
>>> myObj1=MyObj()
>>> myObj1.debug()