为什么Python中的空字符串有时占用49字节,有时占用51字节?

10 浏览
0 Comments

为什么Python中的空字符串有时占用49字节,有时占用51字节?

我在三个环境中测试了sys.getsize('')sys.getsize(' '),在其中两个环境中,sys.getsize('')给出的结果是51字节(比第二个多一个字节),而不是49字节:

截图:

Win8 + Spyder + CPython 3.6:

sys.getsizeof('') == 49 and sys.getsizeof(' ') == 50

Win8 + Spyder + IPython 3.6:

sys.getsizeof('') == 51 and sys.getsizeof(' ') == 50

Win10 (VPN远程) + PyCharm + CPython 3.7:

sys.getsizeof('') == 51 and sys.getsizeof(' ') == 50

第一次编辑

我在Python.exe中进行了第二次测试,而不是在Spyder和PyCharm中(这两个仍然显示为51),一切似乎都很正常。显然我没有解决这个问题的专业知识,所以我把它留给你们解决吧 🙂

Win10 + Python 3.7控制台与使用相同解释器的PyCharm进行对比:

enter image description here

Win8 + IPython 3.6 + 使用相同解释器的Spyder:

enter image description here

0