1. 小整数对象池
整数在程序中的使用非常广泛,Python为了优化速度,使用了小整数对象池, 避免为整数频繁申请和销毁内存空间。
Python 对小整数的定义是 [-5, 256] 这些整数对象是提前建立好的,不会被垃圾回收。在一个 Python 的程序中,无论这个整数处于LEGB中的哪个位置,
所有位于这个范围内的整数使用的都是同一个对象。同理,单个字母也是这样的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
In [ 1 ]: a = - 5
In [ 2 ]: b = - 5
In [ 3 ]: a is b
Out[ 3 ]: True
In [ 4 ]: a = 256
In [ 5 ]: b = 256
In [ 6 ]: a is b
Out[ 6 ]: True
In [ 7 ]: a = 1000
In [ 8 ]: b = 1000
In [ 9 ]: a is b
Out[ 9 ]: False
intern 机制处理空格一个单词的复用机会大,所以创建一次,有空格创建多次,但是字符串长度大于 20 ,就不是创建一次了。
In [ 13 ]: a = "abc"
In [ 14 ]: b = "abc"
In [ 15 ]: a is b
Out[ 15 ]: True
In [ 16 ]: a = "helloworld"
In [ 17 ]: b = "helloworld"
In [ 18 ]: a is b
Out[ 18 ]: True
In [ 19 ]: a = "hello world"
In [ 20 ]: b = "hello world"
In [ 21 ]: a is b
Out[ 21 ]: False
s1 = "abcd"
s2 = "abcd"
print (s1 is s2)
s1 = "a" * 20
s2 = "a" * 20
print (s1 is s2)
s1 = "a" * 21
s2 = "a" * 21
print (s1 is s2)
s1 = "ab" * 10
s2 = "ab" * 10
print (s1 is s2)
s1 = "ab" * 11
s2 = "ab" * 11
print (s1 is s2)
# True
# True
# False
# True
# False
|
2.大整数对象池。说明:终端是每次执行一次,所以每次的大整数都重新创建,而在pycharm中,每次运行是所有代码都加载都内存中,属于一个整体,所以
这个时候会有一个大整数对象池,即处于一个代码块的大整数是同一个对象。c1 和d1 处于一个代码块,而c1.b和c2.b分别有自己的代码块,所以不相等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
C1.b is C2.b
In [ 22 ]: a = 1000
In [ 23 ]: b = 1000
In [ 24 ]: a is b
Out[ 24 ]: False
In [ 25 ]: a = - 1888
In [ 26 ]: b = - 1888
In [ 27 ]: a is b
Out[ 27 ]: False
In [ 28 ]:
c1 = 1000
d1 = 1000
print (c1 is d1) # True
class C1( object ):
a = 100
b = 100
c = 1000
d = 1000
class C2( object ):
a = 100
b = 1000
print (C1.a is C1.b) # True
print (C1.a is C2.a) # True
print (C1.c is C1.d) # True
print (C1.b is C2.b) # False
|
以上这篇对Python中小整数对象池和大整数对象池的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wangyunfeis/article/details/77607156