python标准库及内置函数及其他要点概念解析

  1. defaultdict. 我们知道,可以通过d=defaultdict(int)来实现对value的默认复制,那么如果想使用其他默认值,怎么办呢?—— d=defaultdict(lambda x=10:x)

2.namedtuple.

from collections import Counter, defaultdict,namedtuple

D = namedtuple('D','age name')

a = D('12','oyj')

print(a.age)

3.map(a,b). a为一个func,b为可迭代对象。对b中的每一个对象应用函数执行,得到一个列表.如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)

image.png

4.Python标准库:内置函数next(iterator[, default])::本函数是返回迭代子下一个元素的值,主要通过调用next()方法来实现的。如果default参数有设置,当下一个元素不存在时,就返回default参数的值,否则抛出异常StopIteration。

  1. 用python自带的标准库pickle来存储python原生对象到文件中(文件以二进制形式打开),pickle.dump and pickle.load

6.在python中,要注意共享引用的带来的影响。当你要copy一个列表,你可以使用L[:]或者list(L);当你要copy一个dict时候,你可以使用dict.copy()。但是要注意的是,这两种方法都只能做到顶层复制,也就是说,不能够复制嵌套的数据结构。如果你需要的话,可以使用copy.deepcopy(obj)

7.你可以预先给一个列表赋予一定的数量以方便进行索引,eg: a = [None] * 100。 这样你就可以在0-99的索引上进行操作。

Paste_Image.png

9.在python2中,True并不是keyword,而1是一个被优化了的constant,所以while 1 is faster than while True in python2 。 But, in python3, True is truely become a keyword。所以,在python3中我们是可以使用while True 的,在 大量 Loop 的情景

Paste_Image.png
image.png
  1. unichr()和chr()函数功能基本一样, 只不过是返回unicode的字符

  2. The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
    The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

image.png
  1. Freezing tools comparison
image.png
  1. Difference between os.getenv and os.environ.get?

  2. Why can't Python's raw string literals end with a single backslash?

  3. Implement repr for any class you implement. This should be second nature. Implement str if you think it would be useful to have a string version which errs on the side of more readability in favor of more ambiguity.
    if repr is defined, and str is not, the object will behave as though str=repr.
    Difference between str and repr in Python

19.setattr方法
会拦截所有属性的的赋值语句。如果定义了这个方法,self.arrt = value 就会变成self,setattr("attr", value).这个需要注意。当在setattr方法内对属性进行赋值是,不可使用self.attr = value,因为他会再次调用self,setattr("attr", value),则会形成无穷递归循环,最后导致堆栈溢出异常。应该通过对属性字典做索引运算来赋值任何实例属性,也就是使用self.dict['name'] = value.或者object.setattr(self,key,value)

  1. 对类实例进行bool运算,需要在类内定义 bool method.(在py2中为 nonzero)

  2. Type 的两种usages
    type(A) 返回的是生成对象A的类名。
    B = type('B',(),{}) 将会生成一个类名为B的类,并指向变量B。
    相当于class B: pass
    一句话比较好的概括了:元类生成类,类生成对象,元类包含type和type的子类。

  3. hasattr(),getattr(),setattr()

  4. import sys <==>sys = import('sys')

image.png
  1. egg 和 wheel 都是python built distribution 的两种形式,只是现在wheel现在取代egg成为了发行形式标准。本质上,默认格式是Unix上的gzip压缩文件(.tar.gz)和Windows上的ZIP文件。(可以通过更改extension 来查看)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,776评论 0 33
  • The Python Data Model If you learned another object-orien...
    plutoese阅读 1,782评论 0 51
  • Python内置函数详解——总结篇 ** 引 言** ** 数学运算** abs:求数值的绝对值>>> abs...
    yutiansut阅读 791评论 0 1
  • 今天是长假的第七天,已接近尾声,没有了开头的兴奋感,心头多的是沧桑感,今晚就要从老家启程去工作的城市了,心中有...
    yoga唯阅读 332评论 0 2
  • 今天这个有人欢喜有人愁,有人表白有人分手,和每天都是一样的24小时1440分钟多一分也不多,少一分也不多的日子,借...
    云朵儿z阅读 312评论 0 1