iswbm / magic-python

Python 黑魔法手册
http://magic.iswbm.com/
3.32k stars 478 forks source link

魔法冷知识中的几个问题 #7

Closed TinyHandsome closed 3 years ago

TinyHandsome commented 3 years ago

10.哪些情况下不需要续行符 中:

在多行文本注释中,续行符也是可以不写的,但是会变成\n。

text = '''talk is cheap, show me the code.'''

输出:'talk is cheap,\nshow me the code.'

11.用户无感知的小整数池

在同一行里,同时给两个变量赋同一值时,结果依然为False。

a = 257; b = 257 a is b

输出:False

12.神器的intern机制

如果一个字符串长度超过20个字符,不启动intern机制,这个数字好像不是20

s1 = "a"*100 s2 = "a"*100 s1 is s2

输出:True

s1 = "a"*10001 s2 = "a"*10001 s1 is s2

输出:False

iswbm commented 3 years ago

续行符那个我补充上了。 intern 机制那个,我在 Python 2 和 Python 3.6 中上都试过了是 20 。你是使用 Python 交互式模式运行的吗?还是在 IDE 中测试的?

TinyHandsome commented 3 years ago

我在jupyterlab中运行的。