如何理解python中的池化
PHP中文网
PHP中文网 2017-04-17 17:33:18
0
3
507

在阅读雨痕的python学习笔记第二版中有这样一段话,并没有更多的解释,搜索之后也没有更多相关的资料。
个人感觉是对应字符串对象的一些属性进行修改或者精简,想请教一下大家XD~

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
迷茫

Generally speaking, pooling is a pre-allocation mechanism, such as memory pool, thread pool, etc., which is to reduce the overhead caused by frequent resource requests during operation, but this is obviously not what it means, at least from the context, Pooling in the notes refers to two different meanings, one is the attributes of the class, and the other is the memory layout of constant resources.
1) __name__ and __doc__ should mean that these attributes belong to the class, not the attributes of the instantiated object, such as

from PyQt4.QtCore import *

print QString.__doc__

a = QString("hello")

print a.__doc__

The corresponding __doc__ can be indexed by using the QString class here. Each instantiated object can also access this attribute, but obviously it saves resources to store the data in the class and share it with all instantiated objects.

2) Personally, I feel that the example of intern is not very good. This intern should be a method of String from java (or it was originally introduced by java with reference to python. As for who comes first, there is no way to know), see

http://blog.sina.com.cn/s/blog_69dcd5ed0...

Python uses id() to mark the address information of each object. Therefore, if the id() of two objects are different, it means that the memory addresses of the two objects are different in terms of memory layout (but other The content may be the same). Therefore, in order to make the memory space referenced by objects with the same content the same, use the intern keyword to achieve this purpose and save memory. See the example below

b = "abc"
a = "".join(["a", "b", "c"])


print a == b # 内容是一致的,b是直接引用一个字符串常量,但a是经过计算后、也就是在运行的过程中生成的
print id(a) == id(b) #但是id不一样

a = intern(a) #使用intern处理下,id一样了

print id(a) == id(b)
PHPzhong

It is recommended to take a look at Python source code analysis
Object pool is a technology that is widely used within Python. You will have a clearer understanding if you look at the source code directly.

PHPzhong

To put it simply, int values ​​in the range of -128 to positive 127 are globally unique. No matter how many times the program reads and writes, these common objects are maintained by the Python interpreter to save memory. Different types have different pools. For strings, Generally, commonly used single characters are placed in the pool, and intern is an operating interface for the pool.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!