先讲故事:
这个词原来是Guerrilla Patch(杂牌军、游击队),意思是这部分不是原装的,后来因为guerilla发音和gorllia(猩猩)相似,就演变成了了 gorllia patch,再到后来变成了现在的monkey patch。
猴子补丁(Monkey Patch):属性在运行时动态替换
import socket
print(socket.socket)
print("after monkey patch")
from gevent import monkey
monkey.patch_socket()
print(socket.socket)
运行结果:
<class 'socket.socket'>
after monkey patch
<class 'gevent._socket3.socket'>
如果自己实现这样的功能可以这样做,例如 time.time() 函数:
import time
print(time.time())
def _time():
return 1234
time.time = _time
print(time.time())
结果:
1561413751.8529987
1234
Introspection:
>>> a = [1,2,3]
>>> b = 'philip'
>>> type(b)
<class 'str'>
>>> isinstance(b,list)
False
>>> isinstance(a,list)
注意在使用 id 的时候需要注意 is 与 == 的区别
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a is b
False
>>> id(a)
1610798064584
>>> id(b)
1610797790280
>>> a == b
True
>>> a[1] is b[1]
True
这段演示代码的核心在最后一行,为什么这个地方用 is 判断的时候结果是 True 呢?缘由在于Python的小数预存机制,感兴趣的读者可以自行查询此机制,还可以自行尝试 a = 1,b = 1,去判断 a is b,你会发现返回 True
1、列表推导(list comprehension):
>>> list1 = [[x,y] for x in range(4) for y in range(3) if y%2 == 0]
>>> list1
[[0, 0], [0, 2], [1, 0], [1, 2], [2, 0], [2, 2], [3, 0], [3, 2]]
注意:如果是(i for i in range(10) if i%2 == 0),则返回的是一个生成器,使用list可以对其转换类型
>>> list2 = (i for i in range(10) if i%2 == 0)
>>> list2
<generator object <genexpr> at 0x000001770B08D360>
>>> list3 = list(list2)
>>> list3
[0, 2, 4, 6, 8]
2、字典推导式(dictionary comprehension):
字典推导式的基本格式是:{ key_expr: value_expr for value in collection if condition }
>>> a = [1,2,3,4,5,6]
>>> b = ['a','b','c','d','e']
>>> dict1 = {key : val for key,val in zip(a,b)}
>>> dict1
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
注意:当 key 多于 val 时,代表多的键就没有对应的值,就不会显示了;同理,如果多余的 val 没有 key 与其对应时,也不会显示了。
The Zen Of Python:
代码演示
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
翻译(括号之中的内容是我自己认为写代码的时候可以注意的地方):
额……你以为这块儿我说完了?其实并没有……,有一个很恶搞的东西,当去查看this的源码的时候,会发现它是这样的:
s = “”“Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcygrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera’g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh’er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna evtug abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg’f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn – yrg’f qb zber bs gubfr!”""
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
print("".join([d.get(c, c) for c in s]))
额……我佛了,作者还搞了一个这样的加密在这里,城会玩(ヾ§  ̄▽)ゞ2333333