第一章
t=(1,2,[30,40]) t[2]+=[50,60] 可以赋值,但是会报错。所以元组的元素最好都是不可变的。dis.dis('s[a] += b')
第二章 - An Array of Sequences
第三章 - 介绍dict和set
第四章 - 介绍Text和Bytes
code point - unicode的码位 string通过不同的字符集编码(encode, 例如utf-8, gb2312)变成 bytes第四章 - Functions as Objects
第五章 -Design Patterns with First-Class Functions
第六章 - Function Decorators and Closures
1· 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
-
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.
第十章 - Sequence Hacking, Hashing, and Slicing
sequence protocol:__len__, __getitem__。 __getitem__(self, index)函数的index参数,可能是<class 'slice'> 或者 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__