有没有办法为 Python 对象的所有成员打印简短版本的文档字符串?
Is there a way to print a short version of the docstring for all members of a Python object?
In Python dir()
returns 当前本地作用域中的名称列表。
__doc__
returns 对象的完整文档字符串。
如何列出当前本地范围内的所有名称并打印每个项目的文档字符串的第一行?
详细说明:对于 import numpy as np
,我想获取 dir(np)
返回的所有名称的简短描述列表,例如print(np.nonzero.__doc__.split('.', 1)[0])
.
我该怎么做?
def print_members(obj):
for key in dir(obj):
value = getattr(obj, key)
doc = (value.__doc__ or '').split('.', 1)[0]
print('MEMBER: %s\nDOCSTRING: %s\n\n' % (key, doc))
我用过类似的东西。它很相似,但不完全是你要找的。您可以根据需要调整文档字符串输出。
###############################################################################
def about(obj=None, text=None, capsOnly=False, noLeadingUnderScores=False):
"""
Utility function to assist with discovery while playing in the Python
shell. When possible, returns the sorted dir() and values() of the
specified object.
* noLeadingUnderScores - toggles display filtering of items that have a
leading underscore; only applies to dir() not
values() items.
* capsOnly - toggles display filtering of items that are uppercase only.
this only applies to dir() not values() items.
* text - toggles the display filtering of items that have 'text'
within their string; applies to both dir() and values() items.
"""
print "\n*******************\n* print __obj__ *\n*******************\n"
if obj is None:
print "about() is meaningless as 'obj' is None"
else:
# diplay help(), if possible
try:
if obj.__doc__:
print "\n\n********************\n* HELP() results *\n********************\n"
for x in obj.__doc__.split('\n'):
print x
except:
print "\nno __obj__ available"
# display dir(), if possible
print "\n\n*******************\n* DIR() results *\n*******************\n"
for x in sorted(dir(obj)):
temp = "%s" % x
if noLeadingUnderScores and len(temp) > 0 and temp[0] == "_":
continue
elif capsOnly:
if temp == temp.upper():
if text and text in temp:
print temp
else:
continue
elif text:
if text in temp:
print temp
else:
print temp
# display values(), is possible
try:
if obj.values and type(obj.values) == type({}):
print "\n\n**********************\n* DICT values(k,v) *\n**********************\n"
for x in sorted(obj.values.keys()):
if text:
if text in x or text in str(obj.values[x]):
print x, obj.values[x]
else:
print x, obj.values[x]
except:
print "\nno dictionary like obj.values available"
In Python dir()
returns 当前本地作用域中的名称列表。
__doc__
returns 对象的完整文档字符串。
如何列出当前本地范围内的所有名称并打印每个项目的文档字符串的第一行?
详细说明:对于 import numpy as np
,我想获取 dir(np)
返回的所有名称的简短描述列表,例如print(np.nonzero.__doc__.split('.', 1)[0])
.
我该怎么做?
def print_members(obj):
for key in dir(obj):
value = getattr(obj, key)
doc = (value.__doc__ or '').split('.', 1)[0]
print('MEMBER: %s\nDOCSTRING: %s\n\n' % (key, doc))
我用过类似的东西。它很相似,但不完全是你要找的。您可以根据需要调整文档字符串输出。
###############################################################################
def about(obj=None, text=None, capsOnly=False, noLeadingUnderScores=False):
"""
Utility function to assist with discovery while playing in the Python
shell. When possible, returns the sorted dir() and values() of the
specified object.
* noLeadingUnderScores - toggles display filtering of items that have a
leading underscore; only applies to dir() not
values() items.
* capsOnly - toggles display filtering of items that are uppercase only.
this only applies to dir() not values() items.
* text - toggles the display filtering of items that have 'text'
within their string; applies to both dir() and values() items.
"""
print "\n*******************\n* print __obj__ *\n*******************\n"
if obj is None:
print "about() is meaningless as 'obj' is None"
else:
# diplay help(), if possible
try:
if obj.__doc__:
print "\n\n********************\n* HELP() results *\n********************\n"
for x in obj.__doc__.split('\n'):
print x
except:
print "\nno __obj__ available"
# display dir(), if possible
print "\n\n*******************\n* DIR() results *\n*******************\n"
for x in sorted(dir(obj)):
temp = "%s" % x
if noLeadingUnderScores and len(temp) > 0 and temp[0] == "_":
continue
elif capsOnly:
if temp == temp.upper():
if text and text in temp:
print temp
else:
continue
elif text:
if text in temp:
print temp
else:
print temp
# display values(), is possible
try:
if obj.values and type(obj.values) == type({}):
print "\n\n**********************\n* DICT values(k,v) *\n**********************\n"
for x in sorted(obj.values.keys()):
if text:
if text in x or text in str(obj.values[x]):
print x, obj.values[x]
else:
print x, obj.values[x]
except:
print "\nno dictionary like obj.values available"