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.3.1An 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 implementation 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 detection 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.)
对象不用显式地释放它们; 但是当它们不可用时它就可能会被回收.实现是允许推迟回收或完全忽略它的--这是回收机制如何实现的质量问题. 只要求尚能访问的对象不被回收.(实现注意:当前实现使用一个引用计数机制和一个可选的循环垃圾延时检测机制.只要对象不可用了, 就会收回大部分这样的对象,但不能保证回收中包含有循环引用.对于循环回收的详细控制情况, 见Python 库参考 )
Note that the use of the implementation's tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a `try...except' statement may keep objects alive.
注意使用实现的跟踪和调试工具时可能会保留本该回收的对象, 也要注意使用语句"try ... except"也可能保留本该回收的对象.
Some objects contain references to ``external'' resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a close() method. Programs are strongly recommended to explicitly close such objects. The `try...finally' statement provides a convenient way to do this.
有些对象包含有"外部"资源, 像文件或窗口. 可以相信在垃圾回收时这些资源也被释放. 但因为垃圾回收不保证一定发生, 这样的对象提供了显式的方法释放这些资源. 通常是用close()方法.高度推荐使用这种方法释放包含外部资源的对象."try ... finally"提供了这样的一个途径.
Some objects contain references to other objects; these are called containers. Examples of containers are tuples, lists and dictionaries. The references are part of a container's value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed.
有些对象包含了其它对象的引用; 它们叫做容器, 容器的例子有元组, 列表和字典.引用作为容器值的一部分.大多数情况下, 当我们谈及一个容器的值时, 我们只是涉及了这个值, 而不是所包含的对象; 但是, 当我们谈及容器对象的可变性的时候, 就只是涉及被直接包含的对象的标识了. 因此,如果一个不可变对象(如元组)包含了一个可变对象, 那么只要这个可变对象的值变了则它的值就也改变了.
Types affect almost all aspects of object behavior. Even the importance
of object identity is affected in some sense: for immutable types,
operations that compute new values may actually return a reference to
any existing object with the same type and value, while for mutable
objects this is not allowed. E.g., after
"a = 1; b = 1",
b may or may not refer to the same object with the
value one, depending on the implementation, but after
"c = ; d = ",
are guaranteed to refer to two different, unique, newly created empty
(Note that "c = d = " assigns the same object to both
类型对对象的大多数行为都有影响, 甚至在某种程度上对对象的标识也有重要的影响:对于不可变对象, 计算新值的运算符可能实际上返回的是一个已存在的具有相同类型和值的对象的引用, 对于可变对象,这是不允许的.例如: 在"a = 1; b = 1"之后, a和b有可能指向一个具有值为1的对象, 这依赖于实现.但"c = ; d = "之后,c和d可以保证是保存了两个不同的, 独立的, 新创建的空列表. (注意"c = d = "是把相同的对象赋给了c和d.)