网络编程
位置:首页>> 网络编程>> Python编程>> 详解python的变量缓存机制

详解python的变量缓存机制

作者:小小垂髫  发布时间:2023-05-11 07:47:45 

标签:python,变量,缓存

变量的缓存机制

变量的缓存机制(以下内容仅对python3.6.x版本负责)

机制

只要有两个值相同,就只开辟一个空间

为什么要有这样的机制

在计算机的硬件当中,内存是最重要的配置之一,直接关系到程序的运行速度和流畅度。在过去计算机内存资源昂贵而小的年代中,程序的内存管理成为编程中的重要技术之一。python没有C/C++中的指针那样的定义可以编程者自主的控制内存的分配,而是有一套自动的内存地址分配和缓存机制。在这个机制当中,可以把一些相同值的变量在内存中指向同一块区域,而不再重新开辟一个空间,这样就达到了节省内存的目的。

详解python的变量缓存机制
详解python的变量缓存机制

python中使用id()函数查看数据的内存地址

number部分

整型

对于整型而言,-5~~正无穷的范围内的相同值的id地址一致


# 在后续的版本中所有的数的id地址都一致

# 相同
print(id(9999999), id(9999999))
print(id(100), id(100))
print(id(-5), id(-5))

# 不同
print(id(-6), id(-6))

浮点型

对于浮点型而言,非负数范围内的相同值id一致


# 相同
print(id(9999999.0), id(9999999.0))
print(id(100.0), id(100.0))

# 不同
print(id(-5.0), id(-5.0))
print(id(-6.0), id(-6.0))

布尔值

对于布尔值而言,值相同测情况下,id一致


# 相同
print(id(True), id(True))
print(id(False), id(False))

复数

复数在(实数+虚数)这样的结构当中永不相同,只有单个虚数相同才会一致


# 相同
print(id(1j), id(1j))
print(id(0j), id(0j))

# 不同
print(id(1234j), id(3456j))
print(id(1+1j), id(1+1j))
print(id(2+0j), id(2+0j))

容器部分

字符串

字符串在相同的情况下,地址相同


# 相同
print(id('hello '), id("hello "))

# 不同
print(id('msr'), id('wxd'))

字符串配合使*号使用有特殊的情况:

乘数为1:只要数据相同,地址就是相同的


# 等于1,和正常的情况下是一样的,只要值相同地址就是一样的
a = 'hello ' * 1
b = 'hello ' * 1
print(a is b)
a = '祖国' * 1
b = '祖国' * 1
print(a is b)

乘数大于1:只有仅包含数字、字母、下划线时地址是相同的,而且字符串的长度不能大于20


# 纯数字字母下划线,且长度不大于20
a = '_70th' * 3
b = '_70th' * 3
c = '_70th_70th_70th'
print(a, id(a), len(a))
print(b, id(b), len(b))
print(c, id(c), len(c))
print(a is b is c)
'''
结果:
_70th_70th_70th 1734096389168 15
_70th_70th_70th 1734096389168 15
_70th_70th_70th 1734096389168 15
True
'''

# 纯数字字母下划线,长度大于20
a = 'motherland_70th' * 3
b = 'motherland_70th' * 3
c = 'motherland_70thmotherland_70thmotherland_70th'
print(a, id(a), len(a))
print(b, id(b), len(b))
print(c, id(c), len(c))
print(a is b is c)
'''
结果:
motherland_70thmotherland_70thmotherland_70th 2281801354864 45
motherland_70thmotherland_70thmotherland_70th 2281801354960 45
motherland_70thmotherland_70thmotherland_70th 2281801354768 45
False
'''

# 有其它字符,且长度不大于20
a = '你好' * 3
b = '你好' * 3
c = '你好你好你好'
print(a, id(a), len(a))
print(b, id(b), len(b))
print(c, id(c), len(c))
print(a is b is c)
'''
结果:
你好你好你好 3115902573360 6
你好你好你好 3115902573448 6
你好你好你好 3115900671904 6
False
'''

字符串指定驻留

使用sys模块中的intern函数,让变量指向同一个地址,只要字符串的值是相同的,无论字符的类型、长度、变量的数量,都指向同一个内存地址。

语法:intern(string)


from sys import intern

a = intern('祖国70华诞: my 70th birthday of the motherland' * 1000)
b = intern('祖国70华诞: my 70th birthday of the motherland' * 1000)
c = intern('祖国70华诞: my 70th birthday of the motherland' * 1000)
d = intern('祖国70华诞: my 70th birthday of the motherland' * 1000)
e = intern('祖国70华诞: my 70th birthday of the motherland' * 1000)

print(a is b is c is d is e)

元组

元组只有为空的情况下,地址相同


# 相同
print(id(()), id(tuple()))

# 不同
print(id((1, 2)), id((1, 2)))

列表、集合、字典

任何情况下,地址都不会相同


# 列表、非空元组、集合、字典 无论在声明情况下,id表示都不会相同

# 不同
print(id([]), id([]))
print(id(set()), id(set()))
print(id({}), id({}))

来源:https://www.cnblogs.com/msr20666/archive/2021/01/23/14319386.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com