Comments (10)
这个是由于python会在一开始就把一定范围内的数都创建好。
如果你要创建的数在这个范围内,就直接引用python准备好的对象,不再新建,超出这个范围就会新建对象。而257刚好朝纲。
我找到一篇比较好的解释,你可以看看。
from wtfpython-cn.
你好,我测试了3.7.0和3.7.1,确实从3.7开始这条的结果就变了。
多谢你的细心测试,我会在文中注明这点并将你加入到贡献者名单里,欢迎继续给我提issue。
from wtfpython-cn.
from wtfpython-cn.
我发现原文的issue里已经有了对这个问题的讨论。而且有人发现使用 int()
就可以让 3.7 和 3.6 的表现一致。详细原因还需要我再研究下。
from wtfpython-cn.
看了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?
from wtfpython-cn.
@leisurelicht 好的,谢谢。PS: 你这篇解释文章是刚写的吧,还热乎着😅
from wtfpython-cn.
以前从知乎上转下来的,但是都存在本地。为了有个稳定的链接能给你看才上传。
from wtfpython-cn.
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?
from wtfpython-cn.
from wtfpython-cn.
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]: TrueIn [4]: a = 257
In [5]: b = 257
In [6]: a is b
Out[6]: FalseIn [7]: a = -5
In [8]: b = -5
In [9]: a is b
Out[9]: TrueIn [10]: a = -6
In [11]: b = -6
In [12]: a is b
Out[12]: Falsewho can tell me why?
see https://zhuanlan.zhihu.com/p/33907983
from wtfpython-cn.
Related Issues (20)
- 『Strings can be tricky sometimes/微妙的字符串 *』中 问题 HOT 4
- Class attributes and instance attributes/类属性和实例属性 HOT 2
- Subclass relationships/子类关系 * 的翻译 HOT 1
- is is not what it is!/出人意料的is! HOT 1
- is is not what it is!/出人意料的is! 单行赋值,交互式环境执行的版本问题
- 一个未配对的括号 HOT 1
- windows 10 运行 wtfpython 报错 HOT 1
- 333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 HOT 2
- 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 HOT 2
- MacOS pip3.6 安装wtfpython, run wtfpython后报错FileNotFoundError
- A tic-tac-toe where X wins in the first attempt!/一蹴即至! HOT 1
- Section: Strings can be tricky sometimes/微妙的字符串 * 中常量折叠在3.7中已经迁移至新的AST 优化器 HOT 1
- >>> 0.4-0.3 结果为啥是 0.10000000000000003 HOT 4
- 字符串缓存中注释存在错误? HOT 5
- Sync with English version HOT 2
- About Evaluation time discrepancy/执行时机差异 HOT 3
- 对/=运算符的疑惑 HOT 4
- 一个错别字... HOT 3
- Beware of default mutable arguments!/当心默认的可变参数!
- 中文版本的更新问题 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wtfpython-cn.