第第第三三三章章章
!!数数数据据据模模模型型型Data model
3.1 对象, 值和类型Objects, values and types
Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations
between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code
is also represented by objects.)
Python中,对象是数据的抽象,所有Python程序中的数据都表述成对象,或与对象有关的形式. (实际上,
和”冯–诺依曼的”存储程序计算机”模型一致, 连代码也是对象)
Every object has an identity, a type and a value. An object’s identity never changes once it has been created;
you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects;
the id() function returns an integer representing its identity (currently implemented as its address). An object’s
type is also unchangeable.
1
An object’s type determines the operations that the object supports (e.g., “does it
have a length?”) and also defines the possible values for objects of that type. The type() function returns an
object’s type (which is an object itself). The value of some objects can change. Objects whose value can change
are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The
value of an immutable container object that contains a reference to a mutable object can change when the latter’s
value is changed; however the container is still considered immutable, because the collection of objects it contains
cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.)
An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while
dictionaries and lists are mutable.
每个对象都有一个标识, 一个类型和一个值. 一旦对象建立了, 它的标识就不能改变了; 你可以认为它是内
存中的地址.”is”运算符可以用来比较两个对象的标识; id()函数可以获得一个整数形式的对象标识(当前是
是作为地址实现的). 对象的类型也是不可变的, 它用于检测对象支持的操作(例如,””有长度吗?), 也定义了
该种类型的几个值.type()函数返回对象的类型(类型本身也是一个对象). 某些值的对象可以改变, 值可以
改变的对象称为是可变的, 一旦建立后其值就不可变的对象称为是不可变的.(属于不可变对象的包容器在
包括有可变对象的引用时, 当可变对象改变了时, 其实是可变的, 但仍被看作是可变对象, 因为它所包含的
对象集合是不可变的, 所以不可变对象与值不可变是不完全一样的, 它更加微妙)一个对象的可变性由它
的类型决定, 例如. 数值, 串和元组是不可变的, 但字典和列表是可变的.
Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected.
An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implemen-
tation quality how garbage collection is implemented, as long as no objects are collected that are still reachable.
(Implementation note: the current implementation uses a reference-counting scheme with (optional) delayed de-
tection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not
guaranteed to collect garbage containing circular references. See the Python Library Reference for information on
controlling the collection of cyclic garbage.)
对象不用显式地释放它们; 但是当它们不可用时它就可能会被回收.实现是允许推迟回收或完全忽略它
1
Since Python 2.2, a gradual merging of types and classes has been started that makes this and a few other assertions made in this manual not
100% accurate and complete: for example, it is now possible in some cases to change an object’s type, under certain controlled conditions.
Until this manual undergoes extensive revision, it must now be taken as authoritative only regarding “classic classes”, that are still the default,
for compatibility purposes, in Python 2.2 and 2.3.
15