首頁  >  文章  >  後端開發  >  打印出python 当前全局变量和入口参数的所有属性

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

WBOY
WBOY原創
2016-06-06 11:26:511796瀏覽

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 TO RESUME...')
del x,y,o


简单用法:

1)打印出python 当前全局变量

cndebug()#

2)打印出当前全局变量和myobj的所有属性

myobj={}

cndebug(myobj)

扩展用法——当作类方法,打印实例的成员

>>> class MyObj():
... debug=cndebug
...
>>> myObj1=MyObj()
>>> myObj1.debug()

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn