Python Object

这是我在看 “Learning Python 5 edition” 感受最深刻的一点: everything is object, the type of an object is an object of type type

学过 C 的读者可以将所有 variable 视为指向内存中一个 Object 的 void * 类型指针,指针即引用(reference)。这样 variable 就可以是 C 语言意思上任意类型了,但我们必须指出 , type 是说 object 的 type, variable 没有 type 的说法。 还有 immutable object 和 mutable object 的不同千万别混淆了。

1. variable and object

  1. Variable creation
    A variable (i.e., name), like a , is created when your code first assign it a value. Future assignments change the value of the already created name. Technically, Python detects some names before your code runs, but you can think of it as though initial assignments make variables.

  2. Variable types
    A variable never has any type information or constraints associated with it. The notion of types lives with objects, not names. Variables are generic in nature; they always simply refer to a particular object at a particular point in time.
    感觉这段话说的真是发人深省啊。也就是说,variable 只是一个 name, 它只是一个程序中的一个标识,不存在 type 的概念,Python 也会申请内存来存储这个 variable 的名称,但它并没有和任何 object 有联系。而 object 是内存中实际存在的对象,它有具体的 type。但我们并不能直接指向一个没有任何标识的 object,因此需要使用一个 variable 指向它。当没有任何一个 variable 指向它的时候,它就会被回收掉。

  3. Variable use
    When a variable appears in an expression, it is immediately replaced with the object that it currently refers to, whatever that may be. Further, all variables must be explicitly assigned before they can be used; referencing unassigned variables results in errors.

举个例子:

at least conceptually, Python will perform three distinct steps to carry out the request. These steps reflect the operation of all assignments in the Python language:

  1. Create an object to represent the value 3
  2. Create the variable a, if it does not yet exist.
  3. Link the variable a to the new object 3.

As sketched, variables and objects are stored in different parts of memory and are associated by link

Variables always link to objects and never to other variables, but larger objects may link to other objects (for instance, a list object has links to the objects it contains)

These links from variables to objects are called references in Python – that is, a link is a kink of association, implemented as a pointer in memory.

  • Variables are entries in a system table, with spaces for links to objects.
  • Objects are pieces of allocated memory, with enough space to represent the values for which they stand.
  • References are automatically followed pointers from variables to objects.

Technically speaking, objects have more structure than just enough space to represent their values. Each object also has two standard header fields: a type designator used to mark the type of the object, and a reference counter used to determine when it’s OK to reclaim the object.

In Python variables are always pointers to objects, not labels of changeable memory areas: setting a variables to a new values does not alter the original object, but rather causes the variable to reference entirely different object.
But !!!!
Python’s mutable types, including lists, dictionaries, and sets, perform in-place object changes. For instance, an assignment to an offset in a list actually changes the list object itself in place, rather than generating a brand-new list object.

如何避免 ? 使用 copy() 函数

2. everything is object

In fact, even types themselves are an object type in Python: the type of an object is an object of type type

a call to the built-in function type(X) returns the type object of object X .

3. == and is

the == operator, test whether the two referenced objects have the same value;

the is operator, instead tests for object identity – it returns True only if both names points to the exact same object

But !!!!

Because Python internally caches and reuses some strings as an optimization, there really is just a single string ‘spam’ in memory, shared by S1 and S2 ; hence, the is identity test reports a true result.

To trigger the normal behavior, we need to use longer strings:

4. mutable and immutable object

Python 自带的对象类型中,immutable object 只有 3 种: numbers, string (str, bytes), tuple, 其他都是 mutable 的。

如果学过 C 的话,我们可以将 immutable object 视为 const object,一旦初始化,就无法修改。

请记住,不变的是 immutable object, 指向 (reference)它的 name 或者 variable 永远都可以改变 !!!

如果进行运算,变量指向的 object 其实并不会变化,用 C 语言的话说,因为它是 const 修饰的,永远都不能改变 (当然,引用计数为0,会被垃圾回收)。Python 干的活,是将该变量指向一个新的 immutable object,该 object 的值为计算后的新值。

如果是 mutable object, 那么会有 in-place change, 这和 C 的数组 (array) 非常类似。如果两个 names 指向同一个 mutable object, 并且 object 的值通过其中一个 name 发生改变,通过另一个 name 引用该 object 时,因为还是这个 object, 值自然会更新到最新的

Changing a mutable object in-place can impact other references to that object

请注意,Python 函数的参数传递中,

  1. Immutable arguments are effectively passed “by value”
  2. Mutable arguments are effectively passed “by pointer”

如何想避免 mutable object 的值发生改变,可以拷贝传递,

或者在函数内部先拷贝,在运算

5. cyclic data structures

if a collection object contains a reference to itself, it’s called a cyclic object . Python prints a [...] whenever it detects a cycle in the object, rather than getting stuck in an infinite loop

The solution is knowledge: don’t use cyclic references unless you really need to, and make sure you anticipate them in programs that must care. There are good reasons to create cycles, but unless you have code that knows how to handle them, objects that reference themselves may be more surprise than asset.

发表评论

电子邮件地址不会被公开。 必填项已用*标注