设计模式(一): 原型模式

Java 回顾 Java 访问权限控制 作用域 当前类 同一 package 子类 其他 package public ✔ ✔ ✔ ✔ protected ✔ ✔ ✔ 包作用域 (默认) ✔ ✔ private ✔ 方法覆盖 Method Override 方法覆盖中有一些必须要满足的条件, 这些条件基本都是为了保证多态的有效性, 或者说为了符合 里氏替换原则. ...

September 29, 2016

由Python的super()函数想到的

一直都没搞懂Python中的super函数的工作原理, 决定好好地学习一下, 于是就有了下面这篇文章. 首先看一下super()函数的定义: super([type [,object-or-type]]) Return a **proxy object** that delegates method calls to a **parent or sibling** class of type. 返回一个代理对象, 这个对象负责将方法调用分配给第一个参数的一个父类或者同辈的类去完成. parent or sibling class 如何确定? 第一个参数的__mro__属性决定了搜索的顺序, super指的的是 MRO(Method Resolution Order) 中的下一个类, 而不一定是父类! super()和getattr() 都使用**__mro__**属性来解析搜索顺序, __mro__实际上是一个只读的元组. MRO中类的顺序是怎么排的呢? 实际上MRO列表本身是根据一种C3的线性化处理技术确定的, 理论说明可以参考这里, 这里只简单说明一下原则: 在MRO中, 基类永远出现在派生类的后面, 如果有多个基类, 基类的相对顺序不变. MRO实际上是对继承树做层序遍历的结果, 把一棵带有结构的树变成了一个线性的表, 所以沿着这个列表一直往上, 就可以无重复的遍历完整棵树, 也就解决了多继承中的Diamond问题. 比如说: class Root: pass class A(Root): pass class B(Root): pass class C(A, B): pass print(C.__mro__) # (<class '__main__....

July 10, 2016

HTTP for Human: 深入研究 Requests

这是一篇学习 Requests 的随笔. Python 回顾 bytearray是一个字节数组, 数组的内容是可变的, 数组中的每一个元素都是 [0, 256)之间的整数 定义为: bytearray([source[, encoding[, errors]]]) 其中source参数有几种情况: If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode(). If it is an integer, the array will have that size and will be initialized with null bytes. If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array....