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

yipeiwu_com6年前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里面运用多继承方法详解

在python里面运用多继承方法详解

如何在PYTHON里面运用多继承 class Father: def hobby(self): print("love to play video game.")...

Python测试网络连通性示例【基于ping】

Python测试网络连通性示例【基于ping】

本文实例讲述了Python测试网络连通性。分享给大家供大家参考,具体如下: Python代码 #!/usr/bin/python # -*- coding:GBK -*- """Do...

python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法

python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法

1. test.txt文件,数据以逗号分割,第一个数据为x坐标,第二个为y坐标,数据如下:1.1,2 2.1,2 3.1,3 4.1,5 40,38 42,41 43,42 2....

python笔记(2)

继续List: 删除元素: 复制代码 代码如下: a =[1, 2, 3, 4] a[2:3] = [] #[1, 2, 4] del a[2] #[1, 2] 清空list 复制代码...

python求斐波那契数列示例分享

复制代码 代码如下:def getFibonacci(num): res=[0,1] a=0 b=1 for x in range(0,num):...