在C#中为了提高性能使用了字符串驻留技术,而在Python中不光是字符串,连整数都有使用类似的驻留技术哦,看下面的测试:
C:\Documents and Settings\Marble Wu>python Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a=1 >>> b=1 >>> id(1) 10446048 >>> id(a) 10446048 >>> import sys >>> sys.getrefcount(a) 245 >>> sys.getrefcount(b) 245 >>>
其实也很容易理解,因为在Python一切皆对象