打印出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()

相关文章

python编写暴力破解zip文档程序的实例讲解

python编写暴力破解zip文档程序的实例讲解

编写暴力破解Zip文件要从学习zipfile库的使用方法入手,首先打开Python解释器,用help('zipfile')命令来了解这个库并重点看一下ZipFile类中的extracta...

浅析Python 3 字符串中的 STR 和 Bytes 有什么区别

浅析Python 3 字符串中的 STR 和 Bytes 有什么区别

Python2的字符串有两种:str和Unicode,Python3的字符串也有两种:str和Bytes。Python2的str相当于Python3的Bytes,而Unicode相当于P...

Python3.4实现从HTTP代理网站批量获取代理并筛选的方法示例

本文实例讲述了Python3.4实现从HTTP代理网站批量获取代理并筛选的方法。分享给大家供大家参考,具体如下: 最近在写爬虫,苦于不采用代理的情况下,默认的IP不出几分钟就被封了,故而...

python实现将多个文件分配到多个文件夹的方法

如下所示: import os import shutil #path of imgr path = 'D:\\BaiduNetdiskDownload\\newim\\' #p...

django实现支付宝支付实例讲解

django实现支付宝支付实例讲解

安装python-alipay-sdk pip install python-alipay-sdk --upgrade 配置 视图函数orders/views.py # 订...