<tfoot id='E020I'></tfoot>
      <bdo id='E020I'></bdo><ul id='E020I'></ul>
  • <legend id='E020I'><style id='E020I'><dir id='E020I'><q id='E020I'></q></dir></style></legend>
    <i id='E020I'><tr id='E020I'><dt id='E020I'><q id='E020I'><span id='E020I'><b id='E020I'><form id='E020I'><ins id='E020I'></ins><ul id='E020I'></ul><sub id='E020I'></sub></form><legend id='E020I'></legend><bdo id='E020I'><pre id='E020I'><center id='E020I'></center></pre></bdo></b><th id='E020I'></th></span></q></dt></tr></i><div id='E020I'><tfoot id='E020I'></tfoot><dl id='E020I'><fieldset id='E020I'></fieldset></dl></div>

    <small id='E020I'></small><noframes id='E020I'>

      1. Python 3.6.5 “是"和“=="对于超出缓存间隔的整数

        Python 3.6.5 quot;isquot; and quot;==quot; for integers beyond caching interval(Python 3.6.5 “是和“==对于超出缓存间隔的整数)
        • <i id='G4grb'><tr id='G4grb'><dt id='G4grb'><q id='G4grb'><span id='G4grb'><b id='G4grb'><form id='G4grb'><ins id='G4grb'></ins><ul id='G4grb'></ul><sub id='G4grb'></sub></form><legend id='G4grb'></legend><bdo id='G4grb'><pre id='G4grb'><center id='G4grb'></center></pre></bdo></b><th id='G4grb'></th></span></q></dt></tr></i><div id='G4grb'><tfoot id='G4grb'></tfoot><dl id='G4grb'><fieldset id='G4grb'></fieldset></dl></div>
            <bdo id='G4grb'></bdo><ul id='G4grb'></ul>
            <tfoot id='G4grb'></tfoot>

              <tbody id='G4grb'></tbody>

                  <legend id='G4grb'><style id='G4grb'><dir id='G4grb'><q id='G4grb'></q></dir></style></legend>

                  <small id='G4grb'></small><noframes id='G4grb'>

                  本文介绍了Python 3.6.5 “是"和“=="对于超出缓存间隔的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想先说一下我知道 ==is 之间的区别,一个是引用,另一个是对象.我也知道 python 在启动时缓存 (-5, 256) 范围内的整数,因此在将它们与 is 进行比较时它们应该可以工作.

                  I want to preface this by saying that I know the difference between == and is one is for references and the other is for objects. I also know that python caches the integers in the range (-5, 256) at startup so they should work when comparing them with is.

                  但是我看到了一个奇怪的行为.

                  However I have seen a strange behaviour.

                  >>> 2**7 is 2**7
                  True
                  >>> 2**10 is 2**10
                  False
                  

                  这是意料之中的,2**71282**101024,一个在区间 (-5, 256) 中,另一个不在.

                  This is to be expected, 2**7 is 128 and 2**10 is 1024, one is in the interval (-5, 256) and the other is not.

                  不过……

                  >>> 10000000000000000000000000000000000000000 is 10000000000000000000000000000000000000000
                  True
                  

                  为什么返回 True?这显然是一个高于任何类型缓存间隔的值,并且 2**10 is 2**10 清楚地表明 is 实际上不适用于 以上的整数256.那么……为什么会这样呢?

                  Why does this return True? It is obviously a value WAY above any kind of caching interval and 2**10 is 2**10 clearly showed that is does actually not work on integers above 256. So... why does this happen?

                  推荐答案

                  CPython 检测代码中的常量值并重用它们以节省内存.这些常量存储在代码对象中,甚至可以从python内部访问:

                  CPython detects constant values in your code and re-uses them to save memory. These constants are stored on code objects, and can even be accessed from within python:

                  >>> codeobj = compile('999 is 999', '<stdin>', 'exec')
                  >>> codeobj
                  <code object <module> at 0x7fec489ef420, file "<stdin>", line 1>
                  >>> codeobj.co_consts
                  (999, None)
                  

                  你的 is 的两个操作数都引用这个相同的 999 整数.我们可以通过使用 dis 模块:

                  Both operands of your is refer to this very same 999 integer. We can confirm this by dissecting the code with the dis module:

                  >>> dis.dis(codeobj)
                    1           0 LOAD_CONST               0 (999)
                                2 LOAD_CONST               0 (999)
                                4 COMPARE_OP               8 (is)
                                6 POP_TOP
                                8 LOAD_CONST               1 (None)
                               10 RETURN_VALUE
                  

                  如您所见,前两个 LOAD_CONST 指令均加载索引为 0 的常量,即 999 号.

                  As you can see, the first two LOAD_CONST instructions both load the constant with index 0, which is the 999 number.

                  但是,这只有在两个数字同时编译时才会发生.如果您在单独的代码对象中创建每个数字,它们将不再相同:

                  However, this only happens if the two numbers are compiled at the same time. If you create each number in a separate code object, they will no longer be identical:

                  >>> x = 999
                  >>> x is 999
                  False
                  

                  这篇关于Python 3.6.5 “是"和“=="对于超出缓存间隔的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

                  <small id='a3s06'></small><noframes id='a3s06'>

                • <tfoot id='a3s06'></tfoot>
                  • <legend id='a3s06'><style id='a3s06'><dir id='a3s06'><q id='a3s06'></q></dir></style></legend>

                        <tbody id='a3s06'></tbody>
                          <i id='a3s06'><tr id='a3s06'><dt id='a3s06'><q id='a3s06'><span id='a3s06'><b id='a3s06'><form id='a3s06'><ins id='a3s06'></ins><ul id='a3s06'></ul><sub id='a3s06'></sub></form><legend id='a3s06'></legend><bdo id='a3s06'><pre id='a3s06'><center id='a3s06'></center></pre></bdo></b><th id='a3s06'></th></span></q></dt></tr></i><div id='a3s06'><tfoot id='a3s06'></tfoot><dl id='a3s06'><fieldset id='a3s06'></fieldset></dl></div>
                          • <bdo id='a3s06'></bdo><ul id='a3s06'></ul>