打印出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队列通信:rabbitMQ的使用(实例讲解)

python队列通信:rabbitMQ的使用(实例讲解)

(一)、前言 为什么引入消息队列? 1.程序解耦 2.提升性能 3.降低多业务逻辑复杂度 (二)、python操作rabbit mq rabbitmq配置安装基本使用参见上节文章,不再复...

对Python中9种生成新对象的方法总结

先定义一个类: class Point: def __init__(self, x, y): self.x = x self.y = y 下面我们使用9种方法来生...

Python中变量的输入输出实例代码详解

Python中变量的输入输出实例代码详解

1.变量的输入: input函数:   input()   input("请输入银行卡密码")   password = input("请输入银行卡密码")   变量名 = input...

Python算法输出1-9数组形成的结果为100的所有运算式

问题: 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34–5 + 67–8 + 9 =...

pytorch加载自定义网络权重的实现

在将自定义的网络权重加载到网络中时,报错: AttributeError: 'dict' object has no attribute 'seek'. You can only tor...