Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1753677
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-07-01 19:50:41

4.9. Summary

4.9 小结

The apihelper.py program and its output should now make perfect sense.

整个程序apihelper.py以及它的输出现在都有意义了

 

  1. def info(object, spacing=10, collapse=1):
  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])
  11.  
  12. if __name__ == "__main__":
  13.     print info.__doc__
  14. Here is the output of apihelper.py:

  15. 这是函数的输出

  16. >>> from apihelper import info
  17. >>> li = []
  18. >>> info(li)
  19. append L.append(object) -- append object to end
  20. count L.count(value) -> integer -- return number of occurrences of value
  21. extend L.extend(list) -- extend list by appending list elements
  22. index L.index(value) -> integer -- return index of first occurrence of value
  23. insert L.insert(index, object) -- insert object before index
  24. pop L.pop([index]) -> item -- remove and return item at index (default last)
  25. remove L.remove(value) -- remove first occurrence of value
  26. reverse L.reverse() -- reverse *IN PLACE*
  27. sort L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1

Before diving into the next chapter, make sure you're comfortable doing all of these things:

在继续阅读下章之前,请确保你自己已经掌握了下面的知识点:

  • Defining and calling functions with optional and named arguments
  • 定义以及调用一个带有可选参数的函数
  • Using str to coerce any arbitrary value into a string representation
  • 使用str强制转换任意属性值为一个字符串形式。
  • Using getattr to get references to functions and other attributes dynamically
  • 使用getattr来动态获得函数以及其它属性的引用
  • Extending the list comprehension syntax to do list filtering
  • 扩展list综合操作语法来实现链表过滤
  • Recognizing the and-or trick and using it safely
  • 识别and-or技巧同时能够安全的使用它。
  • Defining lambda functions
  • 定义lambda函数
  • Assigning functions to variables and calling the function by referencing the variable. I can't emphasize this enough, because this mode of thought is vital to advancing your understanding of Python. You'll see more complex applications of this concept throughout this book.
  • 将函数赋值给变量并通过引用变量来调用函数。对于这点,我强调的还不够多,这是因为这中思考方式对于提高你对python的理解至关重要。在本书你会看到更多的关于这种概念的复杂应用。

 

阅读(881) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~