zoukankan      html  css  js  c++  java
  • Python 学习笔记:需要仔细阅读一个函数

     1 def info(object, spacing=10, collapse=True):
     2     """Print methods and doc strings.
     3     
     4     Takes module, class, list, dictionary, or string."""
     5     methodList = [method for method in dir(object) if callable(getattr(object, method))]
     6     processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
     7     print "\n".join(["%s %s" %
     8                       (method.ljust(spacing),
     9                        processFunc(str(getattr(object, method).__doc__)))

    10                      for method in methodList])

    • 用可选和命名参数定义和调用函数
    • 用str强制转换任意值为字符串形式
    • 用getattr动态得到函数和其它属性的引用
    • 扩展列表解析语法实现列表过滤
    • 识别and-or技巧并安全地使用它
    • 使用lambda 函数
  • 相关阅读:
    1组Alpha冲刺总结
    1组Beta冲刺4/5
    1组Beta冲刺5/5
    1组Alpha冲刺4/6
    1组Alpha冲刺总结
    1组Beta冲刺2/5
    1组Beta冲刺3/5
    1组Beta冲刺2/5
    1组Alpha冲刺4/6
    1组Alpha冲刺5/6
  • 原文地址:https://www.cnblogs.com/mobile/p/1372819.html
Copyright © 2011-2022 走看看