fluent python读书笔记

第一章

    1. the python data model - 介绍了三个magic method __getitem__, __len__(内置的会比自己实现的快,因为CPython有优化,因此len不是一个method),还有collections.namedtuple,跟struct类似,挺好用的,用来保存数据增加可读性,类似一个没有操作的class
     2.data strutures。列表推导式,生成器推导式。   [[ '_' ] * for i in range ( 3 )] 和 [['_']*3]*3

t=(1,2,[30,40])  t[2]+=[50,60可以赋值,但是会报错。所以元组的元素最好都是不可变的。dis.dis('s[a] += b'


第二章 - An Array of Sequences

   介绍tuple as records with unnamed fields and as immutable lists 。还有  Named tuples .介绍了一些常用的sequences,list, array, deque

第三章 - 介绍dict和set

  介绍dict和set。用户继承 UserDict 而不是直接继承dict。 MappingProxyType  creates immutable mappings

第四章 - 介绍Text和Bytes

    code point - unicode的码位  string通过不同的字符集编码(encode, 例如utf-8, gb2312)变成 bytes 
    handing text files: 1. bytes decode to str (input)    2. process str    3. str encode to bytes(output)

第四章 - Functions as Objects

  介绍了python中function作为第一公民的一些属性,还有用法
 

第五章 -Design Patterns with First-Class Functions 

1·策略模式,a.setStrategy(strategyBase sb) -> 将策略看做成function,然后调用。适用面 - 不需要保存一些状态信息
2·command模式 

第六章 - Function Decorators and Closures 

decorator run right after the decorated function is defined( import ) 
def A(func):
    return func

@a
def B():
    pass
等价于 B = A(B),所以函数A会执行一次


第七章 - Object References, Mutability,and Recycling 

分清楚immutable和mutable对象的不同。variable其实只是一个label,要清楚它背后的值。函数的传参,其实是对象的alias而已。
list对自身的操作都是影响自己,tuple,string的自身操作都是返回 一个新的对象

  • Simple assignment does not create copies.

  • Augmentedassignmentwith+=or*=createsnewobjectsifthelefthandvariableisbound to an immutable object, but may modify a mutable object in place.

  • Assigning a new value to an existing variable does not change the object previously bound to it. This is called a rebinding: the variable is now bound to a different object.If that variable was the last reference to the previous object, that object will be garbage collected.

  • Function parameters are passed as aliases, which means the function may change any mutable object received as an argument. There is no way to prevent this, except making local copies or using immutable objects (e.g., passing a tuple instead of a list).

  • Using mutable objects as default values for function parameters is dangerous because if the parameters are changed in place, then the default is changed, affecting every future call that relies on the default. 

a = [1,[2,3,4], (5,6,7)]
b = list(a) •Creating a shallow copy (all objects inside are aliases!)

第十章 - Sequence Hacking, Hashing, and Slicing 

sequence protocol:__len__, __getitem__。 __getitem__(self, index)函数的index参数,可能是 或者 int


第十二章 - Inheritance: For Good or For Worse 

1. Distinguish Interface Inheritance from ImplementationInheritance 

2. Make Interfaces Explicit with ABCs

3. Use Mixins for Code Reuse(provide method implementations for reuse ,without implying an “is-a” relationship )

4. Make Mixins Explicit by Naming 

5. An ABC May Also Be a Mixin; The Reverse Is Not True

6. Don’t Subclass from More Than One Concrete Class

7. Provide Aggregate Classes to Users 

8. “Favor Object Composition Over Class Inheritance.” 


第十三章 - Operator Overloading: Doing It Right 

第十四章 -  Iterables, Iterators, and Generators 

1.iterables implements __getitem__ or __iter__

    Pythonobtains iterators from iterables 


2. iterator __next__ and __iter__






你可能感兴趣的:(fluent python读书笔记)