在看Dive Into Python,下面的Python代码是第4章《自省的威力》的示例,运行一下,object可以是list、tuple、dictionary等。然后思考一下,如果用Java或C#写,大概要多少代码呢?另外,”None is None”是啥语言呢?英语?Python?都对!呵呵!
def info(object, spacing=10, collapse=1):
“”"Print methods and doc strings.
Takes module, class, list, dictionary, or string.”"”
methodList = [method for method in dir(object) if callable(getattr(object, method))]
processFunc = collapse and (lambda s: ” “.join(s.split())) or (lambda s: s)
print “\n”.join(["%s %s" %
(method.ljust(spacing),
processFunc(str(getattr(object, method).__doc__)))
for method in methodList])
if __name__ == “__main__”:
print info.__doc__
最新评论