Python编程:从列表操作到面向对象框架
1. 列表操作与 apihelper.py 程序
在Python编程中,列表操作是基础且常用的技能。例如,我们可以使用 join 方法将列表元素以换行符连接并打印出来:
li = ['a', 'b', 'c']
print "\n".join(li)
这段代码的输出为:
a
b
c
这种方法不仅可以用于简单的列表打印,还是调试列表时的实用技巧。
接下来,我们看看 apihelper.py 程序。该程序的核心功能是打印对象的方法和文档字符串,以下是其代码:
def help(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(["
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



