Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section: Strain your brain! --> Strings can be tricky sometimes *中有条示例似乎已经不适用了 #13

Closed
RocShi opened this issue Mar 20, 2019 · 10 comments

Comments

@RocShi
Copy link

RocShi commented Mar 20, 2019

首先,感谢作者的辛勤劳作,实践中发现Strings can be tricky sometimes/微妙的字符串 *中第2段代码段最后一条示例在我的环境中的运行结果与文中恰恰相反,原文中是这样的:

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

我的环境中是这样的:

$ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = "wtf!", "wtf!"
>>> a is b
False

我的操作环境:windows 10企业版(64位操作系统,基于x64处理器),Python 3.7.1(64位)。
请查证。

@leisurelicht
Copy link
Owner

你好,我测试了3.7.0和3.7.1,确实从3.7开始这条的结果就变了。

多谢你的细心测试,我会在文中注明这点并将你加入到贡献者名单里,欢迎继续给我提issue。

@leisurelicht
Copy link
Owner

@leisurelicht
Copy link
Owner

我发现原文的issue里已经有了对这个问题的讨论。而且有人发现使用 int() 就可以让 3.7 和 3.6 的表现一致。详细原因还需要我再研究下。

@RocShi
Copy link
Author

RocShi commented Mar 23, 2019

看了satwikkansal/wtfpython#100 (comment) 这个comment感觉更有意思了,请看Python 3.7.1中下面的示例:

$ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 10
>>> b = 10
>>> a is b
True
>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False

So interesting, but why?

@leisurelicht
Copy link
Owner

这个是由于python会在一开始就把一定范围内的数都创建好。

如果你要创建的数在这个范围内,就直接引用python准备好的对象,不再新建,超出这个范围就会新建对象。而257刚好朝纲。

我找到一篇比较好的解释,你可以看看。

@RocShi
Copy link
Author

RocShi commented Mar 23, 2019

@leisurelicht 好的,谢谢。PS: 你这篇解释文章是刚写的吧,还热乎着😅

@leisurelicht
Copy link
Owner

以前从知乎上转下来的,但是都存在本地。为了有个稳定的链接能给你看才上传。

@jiangbo721
Copy link

Python 3.6.8 (default, Apr 11 2019, 11:52:43)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: a = 256
In [2]: b = 256
In [3]: a is b
Out[3]: True

In [4]: a = 257
In [5]: b = 257
In [6]: a is b
Out[6]: False

In [7]: a = -5
In [8]: b = -5
In [9]: a is b
Out[9]: True

In [10]: a = -6
In [11]: b = -6
In [12]: a is b
Out[12]: False

who can tell me why?

@Charlotte888888
Copy link

Charlotte888888 commented Jan 6, 2020 via email

@oubeichen
Copy link

Python 3.6.8 (default, Apr 11 2019, 11:52:43)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: a = 256
In [2]: b = 256
In [3]: a is b
Out[3]: True

In [4]: a = 257
In [5]: b = 257
In [6]: a is b
Out[6]: False

In [7]: a = -5
In [8]: b = -5
In [9]: a is b
Out[9]: True

In [10]: a = -6
In [11]: b = -6
In [12]: a is b
Out[12]: False

who can tell me why?

see https://zhuanlan.zhihu.com/p/33907983

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants