Code Monkey home page Code Monkey logo

Comments (10)

leisurelicht avatar leisurelicht commented on September 7, 2024 2

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

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

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

from wtfpython-cn.

leisurelicht avatar leisurelicht commented on September 7, 2024

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

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

from wtfpython-cn.

leisurelicht avatar leisurelicht commented on September 7, 2024

satwikkansal/wtfpython#100

from wtfpython-cn.

leisurelicht avatar leisurelicht commented on September 7, 2024

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

from wtfpython-cn.

RocShi avatar RocShi commented on September 7, 2024

看了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.

RocShi avatar RocShi commented on September 7, 2024

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

from wtfpython-cn.

leisurelicht avatar leisurelicht commented on September 7, 2024

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

from wtfpython-cn.

jiangbo721 avatar jiangbo721 commented on September 7, 2024

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.

Charlotte888888 avatar Charlotte888888 commented on September 7, 2024

from wtfpython-cn.

oubeichen avatar oubeichen commented on September 7, 2024

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

from wtfpython-cn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.