PythonPypython效率

学习了6个月总结出来的:50个有趣而又鲜为人知的Python特性

2018-12-01  本文已影响98人  妄心xyx

Python, 是一个设计优美的解释型高级语言, 它提供了很多能让程序员感到舒适的功能特性。但有的时候, Python 的一些输出结果对于初学者来说似乎并不是那么一目了然。

这个有趣的项目意在收集 Python 中那些难以理解和反人类直觉的例子以及鲜为人知的功能特性, 并尝试讨论这些现象背后真正的原理!

虽然下面的有些例子并不一定会让你觉得 WTFs,但它们依然有可能会告诉你一些你所不知道的 Python 有趣特性。我觉得这是一种学习编程语言内部原理的好办法, 而且我相信你也会从中获得乐趣!

目录

Structure of the Examples/示例结构
Usage/用法
Examples/示例
>+= is faster/更快的 +=
> Let's make a giant string!/来做个巨大的字符串吧!
> Explicit typecast of strings/字符串的显式类型转换
> Minor Ones/小知识点
> Okay Python, Can you make me fly?/Python, 可否带我飞? *
> goto, but why?/goto, 但为什么? *
> Brace yourself!/做好思想准备 *
> Let's meet Friendly Language Uncle For Life/让生活更友好 *
> Even Python understands that love is complicated/连Python也知道爱是难言的 *
> Yes, it exists!/是的, 它存在!
> Inpinity/无限 *
> Mangling time!修饰时间! *
> Modifying a dictionary while iterating over it/迭代字典时的修改
> Stubborn del operator/坚强的 del *
> Deleting a list item while iterating/迭代列表时删除元素
> Loop variables leaking out!/循环变量泄漏!
> Beware of default mutable arguments!/当心默认的可变参数!
> Catching the Exceptions/捕获异常
> Same operands, different story!/同人不同命!
> The out of scope variable/外部作用域变量
> Be careful with chained operations/小心链式操作
> Name resolution ignoring class scope/忽略类作用域的名称解析
> Needle in a Haystack/大海捞针
> Skipping lines?/跳过一行?
> Teleportation/空间移动 *
> Well, something is fishy.../嗯, 有些可疑...
> Strings can be tricky sometimes/微妙的字符串 *
> Time for some hash brownies!/是时候来点蛋糕了!
> Return return everywhere!/到处返回!
> Deep down, we're all the same./本质上,我们都一样. *
> For what?/为什么?
> Evaluation time discrepancy/评估时间差异
> is is not what it is!/出人意料的is!
> A tic-tac-toe where X wins in the first attempt!/一蹴即至!
> The sticky output function/麻烦的输出
> is not ... is not is (not ...)/is not ... 不是 is (not ...)
> The surprising comma/意外的逗号
> Backslashes at the end of string/字符串末尾的反斜杠
> not knot!/别纠结!
> Half triple-quoted strings/三个引号
> Midnight time doesn't exist?/不存在的午夜?
> What's wrong with booleans?/布尔你咋了?
> Class attributes and instance attributes/类属性和实例属性
> yielding None/生成 None
> Mutating the immutable!/强人所难
> The disappearing variable from outer scope/消失的外部变量
> When True is actually False/真亦假
> From filled to None in one instruction.../从有到无...
> Subclass relationships/子类关系 *
> The mysterious key type conversion/神秘的键型转换 *
> Let's see if you can guess this?/看看你能否猜到这一点?
Section: Strain your brain!/大脑运动!
Section: Appearances are deceptive!/外表是靠不住的!
Section: Watch out for the landmines!/小心地雷!
Section: The Hidden treasures!/隐藏的宝藏!
Section: Miscellaneous/杂项
Contributing/贡献
Acknowledgements/致谢
License/许可
Help/帮助
Want to surprise your geeky pythonist friends?/想给你的极客朋友一个惊喜?
Need a pdf version?/需要来一份pdf版的?
Follow Commit/追踪Commit

示例结构

所有示例的结构都如下所示:

**> 一个精选的标题 ***

标题末尾的星号表示该示例在第一版中不存在,是最近添加的。

# 准备代码.
# 释放魔法...

Output (Python version):

>>> 触发语句
出乎意料的输出结果

(可选): 对意外输出结果的简短描述。

说明

简要说明发生了什么以及为什么会发生。

如有必要, 举例说明

Output:

>>> 触发语句 # 一些让魔法变得容易理解的例子
# 一些正常的输入

注意: 所有的示例都在 Python 3.5.2 版本的交互解释器上测试过, 如果不特别说明应该适用于所有 Python 版本。

小标题:Usage/用法

我个人建议, 最好依次阅读下面的示例, 并对每个示例:

PS: 你也可以在命令行阅读 WTFpython. 我们有 pypi 包 和 npm 包(支持代码高亮)。(译: 这两个都是英文版的)

安装 npm 包 wtfpython

$ npm install -g wtfpython

或者, 安装 pypi 包 wtfpython

$ pip install wtfpython -U

现在, 在命令行中运行 wtfpython, 你就可以开始浏览了。

小标题:Examples/示例

Section: Strain your brain!/大脑运动!

**> Strings can be tricky sometimes/微妙的字符串 ***

1、

>>> a = "some_string"
>>> id(a)
140420665652016
>>> id("some" + "_" + "string") # 注意两个的id值是相同的.
140420665652016

2、

>>> a = "wtf"
>>> b = "wtf"
>>> a is b
True
>>> a = "wtf!"
>>> b = "wtf!"
>>> a is b
False
>>> a, b = "wtf!", "wtf!"
>>> a is b
True

3、

>>> 'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'
True
>>> 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'
False

很好理解, 对吧?

说明:

学习了6个月总结出来的:50个有趣而又鲜为人知的Python特性

>Time for some hash brownies!/是时候来点蛋糕了!

hash brownie指一种含有大麻成分的蛋糕, 所以这里是句双关

1、

some_dict = {}
some_dict[5.5] = "Ruby"
some_dict[5.0] = "JavaScript"
some_dict[5] = "Python"

Output:

>>> some_dict[5.5]
"Ruby"
>>> some_dict[5.0]
"Python"
>>> some_dict[5]
"Python"

"Python" 消除了 "JavaScript" 的存在?

说明:

>>> 5 == 5.0
True
>>> hash(5) == hash(5.0)
True

Return return everywhere!/到处返回!

def some_func():
 try:
 return 'from_try'
 finally:
 return 'from_finally'

Output:

>>> some_func()
'from_finally'

说明:

Deep down, we're all the same./本质上,我们都一样. *

class WTF:
 pass

Output:

>>> WTF() == WTF() # 两个不同的对象应该不相等
False
>>> WTF() is WTF() # 也不相同
False
>>> hash(WTF()) == hash(WTF()) # 哈希值也应该不同
True
>>> id(WTF()) == id(WTF())
True

说明:

class WTF(object):
 def __init__(self): print("I")
 def __del__(self): print("D")

Output:

>>> WTF() is WTF()
I
I
D
D
False
>>> id(WTF()) == id(WTF())
I
D
I
D
True

正如你所看到的, 对象销毁的顺序是造成所有不同之处的原因。

> For what?/为什么?

some_string = "wtf"
some_dict = {}
for i, some_dict[i] in enumerate(some_string):
 pass

Output:

>>> some_dict # 创建了索引字典.
{0: 'w', 1: 't', 2: 'f'}

说明:

for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]

其中 exprlist 指分配目标. 这意味着对可迭代对象中的每一项都会执行类似 {exprlist} = {next_value} 的操作.

一个有趣的例子说明了这一点:

for i in range(4):
 print(i)
 i = 10

Output:

0
1
2
3

你可曾觉得这个循环只会运行一次?

说明:

>>> i, some_dict[i] = (0, 'w')
>>> i, some_dict[i] = (1, 't')
>>> i, some_dict[i] = (2, 'f')
>>> some_dict

> Evaluation time discrepancy/评估时间差异

1、

>array = [1, 8, 15]
g = (x for x in array if array.count(x) > 0)
array = [2, 8, 22]

Output:

>>> print(list(g))
[8]

2、

array_1 = [1,2,3,4]
g1 = (x for x in array_1)
array_1 = [1,2,3,4,5]
array_2 = [1,2,3,4]
g2 = (x for x in array_2)
array_2[:] = [1,2,3,4,5]

Output:

>>> print(list(g1))
[1,2,3,4]
>>> print(list(g2))
[1,2,3,4,5]

说明

> is is not what it is!/出人意料的is!

下面是一个在互联网上非常有名的例子。

>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = 257; b = 257
>>> a is b
True

说明:

is 和 == 的区别

>>> [] == []
True
>>> [] is [] # 这两个空列表位于不同的内存地址.
False

256 是一个已经存在的对象, 而 257 不是

当你启动Python 的时候, -5 到 256 的数值就已经被分配好了. 这些数字因为经常使用所以适合被提前准备好。

>>> id(256)
10922528
>>> a = 256
>>> b = 256
>>> id(a)
10922528
>>> id(b)
10922528
>>> id(257)
140084850247312
>>> x = 257
>>> y = 257
>>> id(x)
140084850247440
>>> id(y)
140084850247344

这里解释器并没有智能到能在执行 y = 257 时意识到我们已经创建了一个整数 257, 所以它在内存中又新建了另一个对象。

当 a 和 b 在同一行中使用相同的值初始化时,会指向同一个对象。

>>> a, b = 257, 257
>>> id(a)
140640774013296
>>> id(b)
140640774013296
>>> a = 257
>>> b = 257
>>> id(a)
140640774013392
>>> id(b)
140640774013488

> A tic-tac-toe where X wins in the first attempt!/一蹴即至!

Output:

# 我们先初始化一个变量row
row = [""]*3 #row i['', '', '']
# 并创建一个变量board
board = [row]*3

我们有没有赋值过3个 "X" 呢?

>>> board
[['', '', ''], ['', '', ''], ['', '', '']]
>>> board[0]
['', '', '']
>>> board[0][0]
''
>>> board[0][0] = "X"
>>> board
[['X', '', ''], ['X', '', ''], ['X', '', '']]

说明:

当我们初始化 row 变量时, 下面这张图展示了内存中的情况。

学习了6个月总结出来的:50个有趣而又鲜为人知的Python特性

而当通过对 row 做乘法来初始化 board 时, 内存中的情况则如下图所示 (每个元素 board[0], board[1] 和 board[2] 都和 row 一样引用了同一列表。)

学习了6个月总结出来的:50个有趣而又鲜为人知的Python特性

我们可以通过不使用变量 row 生成 board 来避免这种情况. (这个issue提出了这个需求。)

>>> board = [['']*3 for _ in range(3)]
>>> board[0][0] = "X"
>>> board
[['X', '', ''], ['', '', ''], ['', '', '']]

> The sticky output function/麻烦的输出

funcs = []
results = []
for x in range(7):
 def some_func():
 return x
 funcs.append(some_func)
 results.append(some_func())
funcs_results = [func() for func in funcs]

Output:

>>> results
[0, 1, 2, 3, 4, 5, 6]
>>> funcs_results
[6, 6, 6, 6, 6, 6, 6]

即使每次在迭代中将 some_func 加入 funcs 前的 x 值都不相同, 所有的函数还是都返回6。

说明:

funcs = []
for x in range(7):
 def some_func(x=x):
 return x
 funcs.append(some_func)

Output:

>>> funcs_results = [func() for func in funcs]
>>> funcs_results
[0, 1, 2, 3, 4, 5, 6]

is not ... is not is (not ...)/is not ... 不是 is (not ...)

>>> 'something' is not None
True
>>> 'something' is (not None)
False

说明:

上一篇 下一篇

猜你喜欢

热点阅读