出于对性能的考虑,在python IDLE中,数字位于[-5,256],和所有的字符串,值相同的,不同变量会指向同一个内存。
>>> c=257
>>> d=257
>>> c is d
False
>>> c=256
>>> d=256
>>> c is d
True
>>> c=-5
>>> d=-5
>>> c is d
True
#下面是字符串
>>> a='257'
>>> b='257'
>>> a is b
True
>>> a='123456778'
>>> b='123456778'
>>> a is b
True
>>> a='asdfsdfassdfsfafdasdfasdfasdfasdfasfdasdfasdfsdfsadfasdfasdfsdfasdf'
>>> b='asdfsdfassdfsfafdasdfasdfasdfasdfasfdasdfasdfsdfsadfasdfasdfsdfasdf'
>>> a is b
True
pycharm上运行…Python出于对性能的考虑,但凡是不可变对象,在同一个代码块中的对象,只有是值相同的对象,就不会重复创建,而是直接引用已经存在的对象。
这种思想大义,需要实时掌握。