python 浅谈小数据池和编码

. ⼩数据池

  在说⼩数据池之前. 我们先看⼀个概念. 什么是代码块:

根据提示我们从官⽅⽂档找到了这样的说法:
A Python program is constructed from code blocks. A block is a piece of
Python program text that is executed as a unit. The following are blocks: a
module, a function body, and a class definition. Each command typed
interactively is a block. A script file (a file given as standard input to
the interpreter or specified as a command line argument to the interpreter)
is a code block. A script command (a command specified on the interpreter
command line with the ‘-c‘ option) is a code block. The string argument
passed to the built-in functions eval() and exec() is a code block.
A code block is executed in an execution frame. A frame contains some
administrative information (used for debugging) and determines where and
how execution continues after the code block’s execution has completed.
粗略的翻译:
python程序是由代码块构成的. ⼀个代码块的⽂本作为python程序执⾏的单元.
代码块: ⼀个模块, ⼀个函数, ⼀个类, 甚⾄每⼀个command命令都是⼀个代码块. ⼀个⽂件也是⼀
个代码块, eval()和exec()执⾏的时候也是⼀个代码块

二、接下来我们来看一下小数据池is和 ==的区别

  1、id( )

  通过id( )我们可以查看到一个变量表示的值的内存地址

s = 'alex'
print(id(s)) # 4326667072

   2、is和==

    == 判断左右两段的值是否相等,是否一致

    is 判断左右两端的内存地址是否一致,如果一致就返回True

    注意:如果内存地址相同. 那么值⼀定是相等的如果值相等. 则不⼀定是同⼀个对象 。

  3、⼩数据池.:⼀种数据缓存机制. 也被称为驻留机制

     ⼩数据池只针对: 整数, 字符串, 布尔值. 其他的数据类型不存在驻留机制 。

对于整数, Python官⽅⽂档中这么说:
The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object. So it should be
possible to change the value of 1. I suspect the behaviour of Python in
this case is undefined.

对于字符串:
Incomputer science, string interning is a method of storing only onecopy of
each distinct string value, which must be immutable. Interning strings
makes some stringprocessing tasks more time- or space-efficient at the cost
of requiring moretime when the string is created or interned. The distinct
values are stored ina string intern pool. –引⾃维基百科

    在python中对-5到256之间的整数会被驻留在内存中,将一定规则的字符串缓存,在使用的时候,内存中只会创建一个该数据的对象,保存小数据池中,当使用的时候直接从

小数据池中获取对象的内存引用,二不需要创建一个新的数据,这样可以节省更多的内存。

  优点:能够提高一些字符串、整数的处理速度,省去了创建对象的过程。

  缺点:在”池“中插入或者创建新值会花费更多时间。

  对于数字: -5~256是会被加到⼩数据池中的. 每次使⽤都是同⼀个对象.
  对于字符串:
    1. 如果字符串的⻓度是0或者1, 都会默认进⾏缓存
    2. 字符串⻓度⼤于1, 但是字符串中只包含字⺟, 数字, 下划线时才会缓存
    3. ⽤乘法的到的字符串. . 乘数为1, 仅包含数字, 字⺟, 下划线时会被缓存. 如果
     包含其他字符, ⽽⻓度<=1 也会被驻存, . 乘数⼤于1 . 仅包含数字, 字⺟, 下划
        线这个时候会被缓存. 但字符串⻓度不能⼤于20
    4. 指定驻留. 我们可以通过sys模块中的intern()函数来指定要驻留的内容.

a = 1000
b = 1000
print(a is b)
注意. 在py⽂件中.得到的结果是True, 但是在command中就不是了.

  在代码块内的缓存机制是不⼀样的. 在执⾏同⼀个代码块的初始化对象的命令时, 会检查是否其值是否已经存在, 如果存在, 会将其重⽤. 换句话说: 执⾏同⼀个代码块时,

遇到初始化对象的命令时他会将初始化的这个变量与值存储在⼀个字典中, 在遇到新的变量时, 会先在字典中查询记录, 如果有同样的记录那么它会重复使⽤这个字典中的

之前的这个值. 所以在你给出的例⼦中, ⽂件执⾏时(同⼀个代码块) 会把a, b两个变量指向同⼀个对象.如果是不同的代码块, 他就会看这个两个变量是否是满⾜⼩数据池的数据,

如果是满⾜⼩数据池的数据则会指向同⼀个地址. 所以: a, b的赋值语句分别被当作两个代码块执⾏, 但是他们不满⾜⼩数据池的数据所以会得到两个不同的对象, 因⽽is判断

返回False.

  

三、编码

  1. ASCII : 最早的编码. ⾥⾯有英⽂⼤写字⺟, ⼩写字⺟, 数字, ⼀些特殊字符. 没有中⽂,801代码, 8bit, 1byte
  2. GBK: 中⽂国标码, ⾥⾯包含了ASCII编码和中⽂常⽤编码. 16bit, 2byte
  3. UNICODE: 万国码, ⾥⾯包含了全世界所有国家⽂字的编码. 32bit, 4byte, 包含了ASCII
  4. UTF-8: 可变⻓度的万国码. unicode的⼀种实现. 最⼩字符占8
    1.英⽂: 8bit 1byte
    2.欧洲⽂字:16bit 2byte
    3.中⽂:24bit 3byte

  在python3的内存中,在程序运行阶段,使用的是unicode编码。因为unicode是万国码,什么内容都可以进行显示,

那么在数据传输和存储的时候由于unicode比较浪费时间和资源。需要把unicode转村委utf-8或者gbk进行存储。

  在python中可以把文字信息进行编码。编码以后的内容就可以进行传输了,编码以后的数据都是bytes类型的数据,其实

原来的数据只是被编码了,并没有改变信息内容。

  5、bytes的表现形式:

    1、英文 b' alex’ 引文的表现形式跟字符串没什么区别

    2、中文 b'xe4xb8xad‘ 这是一个汉字的utf-8的bytes表现形式。

  字符串在传输时转化成bytes=> encode(字符集)来完成

s = "alex"
print(s.encode("utf-8")) # 将字符串编码成UTF-8
print(s.encode("GBK")) # 将字符串编码成GBK

结果:
b'alex'b'alex'

s = ""
print(s.encode("UTF-8")) # 中⽂编码成UTF-8
print(s.encode("GBK")) # 中⽂编码成GBK

结果:
b'xe4xb8xad'
b'xd6xd0

  解码

s = "我叫李嘉诚"
print(s.encode("utf-8")) #
b'xe6x88x91xe5x8fxabxe6x9dx8exe5x98x89xe8xafx9a'
print(b'xe6x88x91xe5x8fxabxe6x9dx8exe5x98x89xe8xafx9a'.decode("utf-8")) # 解码

  编码和解码的时候都需要制定编码格式.

s = "我是⽂字"
bs = s.encode("GBK") # 我们这样可以获取到GBK的⽂字
# 把GBK转换成UTF-8
# ⾸先要把GBK转换成unicode. 也就是需要解码
s = bs.decode("GBK") # 解码
# 然后需要进⾏重新编码成UTF-8
bss = s.encode("UTF-8") # 重新编码
print(bss)

   

  

原文地址:https://www.cnblogs.com/angle6-liu/p/9986104.html