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

yipeiwu_com5年前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()

相关文章

用pytorch的nn.Module构造简单全链接层实例

python版本3.7,用的是虚拟环境安装的pytorch,这样随便折腾,不怕影响其他的python框架 1、先定义一个类Linear,继承nn.Module import tor...

Python基于多线程操作数据库相关问题分析

本文实例分析了Python多线程操作数据库相关问题。分享给大家供大家参考,具体如下: python多线程并发操作数据库,会存在链接数据库超时、数据库连接丢失、数据库操作超时等问题。 解决...

使用python将大量数据导出到Excel中的小技巧分享

(1) 问题描述:为了更好地展示数据,Excel格式的数据文件往往比文本文件更具有优势,但是具体到python中,该如何导出数据到Excel呢?如果碰到需要导出大量数据又该如何操作呢?...

Python XML转Json之XML2Dict的使用方法

1. Json读写方法 def parseFromFile(self, fname): """ Overwritten to read JSON files. """...

Python内置的字符串处理函数详细整理(覆盖日常所用)

str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str) 例:print '%s...