编码(后续)

⼀. ⼩数据池

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

根据提示我们从官⽅⽂档找到了这样的说法:

  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()执⾏的时候也是⼀个代码块

什么是命令⾏?

我们在控制台CMD中输入python进入的就是python的command模式. 在这⾥也可以写

python的程序.

⽽根据上⾯的官⽅的解释来看. ⼀个command命令就是⼀个python代码块

⼆. 接下来我们来看⼀下⼩数据池 is和==的区别
1. id()
通过id()我们可以查看到⼀个变量表⽰的值在内存中的地址

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

2. is和==
== 判断左右两端的值是否相等. 是不是⼀致.
is 判断左右两端内容的内存地址是否⼀致. 如果返回True, 那可以确定这两个变量使⽤的是同⼀个对象

三. 编码的补充

1. python2中默认使⽤的是ASCII码. 所以不⽀持中⽂. 如果需要在Python2中更改编码.需要在⽂件的开始编写

# -*- encoding:utf-8 -*-

2. python3中: 内存中使⽤的是unicode码.

猜你喜欢

转载自www.cnblogs.com/Ajie-boby/p/10217067.html