Home  >  Article  >  Backend Development  >  Python objects, names, and bindings

Python objects, names, and bindings

大家讲道理
大家讲道理Original
2017-05-28 10:00:571467browse

PythonAdvanced - Object, name and binding

are written in front

Unless otherwise specified, the following are Based on Python3

1, everything is an object

Python philosophy:

Everything in Python is Object

1.1 DataModel-Object, value and type

Object is Python's abstraction of data. PythonAll data in the program is represented by objects or relationships between objects. (In a sense, in keeping with von Neumann's "storage computer" model, the code in Python is also an object.)

Python Every object has an identity, a value and a type. After an object is created, its identity never changes; you can think of the identity as the address of the object in memory. isOperatorCompares the identities of two objects; id()FunctionReturns an integer# representing the identity of the object.

CPython implementation details: In the implementation of the CPython interpreter, the id(x) function returns the stored x Memory address

The type of the object determines the operations supported by the object (for example, does the object have a length?), and also determines the possible values ​​of the object of this type. type()The function returns the type of object (this type itself is also an object). Like its identity, the object's type is immutable[1].

The values ​​of some objects can be changed. Objects whose values ​​can be changed are also called mutable; objects whose values ​​are constant once created are also called immutable(immutable). (When an immutable container object contains a reference to a mutable object, when the mutable object value changes, the immutable container object value is also changed; however, the immutable container object is still considered Immutable, because the collection of values ​​contained by the object is indeed immutable. Therefore, immutability is not strictly equivalent to having immutable values. It is subtle) (Annotation: First, the value of an immutable container object is a collection, a collection. contains references to other objects; then these references can be regarded as addresses. Even if the content pointed to by the address changes, the address itself in the collection does not change, so the immutable container object is still an immutable object. Depends on their type; for example, numbers, strings, and tuples are immutable, but dictionaries and lists are mutable. Objects are never explicitly destroyed; they are garbage collected when they become unreachable. One interpreter implementation allows garbage collection to be delayed or ignored entirely - depending on how garbage collection is implemented, as long as no reachable objects are collected.

CPython implementation details:

CPythonThe interpreter implements delayed detection using reference counting mode Loop Link garbage, this method can recycle most The object is unreachable, but there is no guarantee that the garbage of the circular reference will be collected. See the documentation for the gc module for more information on controlling cyclic garbage collection. Other interpreter implementations differ from CPython, and CPython implementations may change in the future. Therefore the garbage collector cannot be relied upon to reclaim unreachable objects (so file objects should always be closed explicitly.). It should be noted that using the tool's

debugging

tracing function may cause objects that should be recycled to remain alive. Use try...exceptThe statement catching exceptions can also keep the object alive. Some objects reference external resources such as files or windows. It goes without saying that after the object holding the resource is garbage collected, the resource will also be released. However, because there is no mechanism to guarantee that garbage collection will definitely occur, these resource holding objects also provide a way to explicitly release external resources, usually using

close()

Method. It is strongly recommended to release resources explicitly in the program. The try...finally<a href="http://www.php.cn/wiki/207.html" target="_blank"> statement and the </a>with statement provide convenience for releasing resources. <p>Some objects contain references to other objects, these objects are called <strong><em>containers</em></strong>. Tuples, lists and dictionaries are all containers. Part of the container value referenced. Most of the time, when talking about the value of a container, we are referring to the set of object values ​​the container contains, rather than the set of identities of the objects; however, when talking about the mutability of a container, we are implying the identities of the objects it contains. Therefore, if an immutable object (such as a tuple) contains a reference to a mutable object, when the mutable object changes, its value also changes. </p> <p>Types affect most <a href="http://www.php.cn/php/php-tp-behavior.html" target="_blank">behavior</a> of objects. In some cases even the importance of the object's identity is affected: for immutable types, the operation of computing a new value may actually return a reference to an existing object with the same value and type, whereas for mutable objects This is impossible. For example, after the statement <code>a = 1; b = 1 is executed, a and b may or may not refer to the same object with the same value, depending on the interpretation implement. However, after the statement c = []; d = [] is executed, it is guaranteed that c and d will point to different and unique newly created empty lists. (Note that c = d = [] assigns the same object to c and d)

Note: The above is translated from "The Python Language References#Data model#Objects, values, types" version 3.6.1.

1.2 Object Summary

The official documentation has described the Python object in detail, here is a summary.

Three characteristics of objects:

  • Identity identification
    Uniquely identifies the object; immutable; CPythonExplanation The device is implemented as the memory address of the object.
    Operation: id(), built-in function id()The function returns an integer that identifies the object; isCompares the identities of two objects.
    Example:

    >>> id(1)
    1470514832
    >>> 1 is 1
    True
  • Type
    Determines the operations supported by the object, possible values; immutable.
    Operation: type(), the built-in function returns the type of the object
    Example:

    >>> type('a')
    <class></class>
  • Value
    Data, variable/impossible Change
    operation: == operator is used to compare whether the values ​​​​of two objects are equal, and other comparison operators compare the size of objects.
    Example:

    >>> 'python'
    'python'
    >>> 1 == 2
    False

Variable and immutable: It is generally believed that objects with immutable values ​​are immutable objects, and objects with variable values ​​are mutable objects. mutable objects, but be aware that immutable collection objects contain mutable object reference members.

Object in Python:


# -*- coding: utf-8 -*-# filename: hello.py&#39;a test module&#39;author = &#39;Richard Cheng&#39;import sysclass Person(object):    &#39;&#39;&#39; Person class&#39;&#39;&#39;

    def init(self, name, age):        self.name = name        self.age = agedef tset():    print(sys.path)
    p = Person(&#39;Richard&#39;, 20)    print(p.name, &#39;:&#39;, p.age)def main():
    tset()if name == &#39;main&#39;:
    main()


This paragraph PythonThere are many objects in the code, including the hello module object, the created Person class object, and several functions such as test, mainFunction objects, numbers, strings, and even the code itself are also objects.

2. The name is "Variable"

Almost all languages ​​have the term "variable". Strictly speaking, Python Variables should not be called variables, it is more appropriate to call them names.

The following translation is from Code Like a Pythonista: Idiomatic Python # Python has "names"

2.1 Other languages ​​have variables

In other languages, assigning values ​​to variables is like The value is placed in the "box".
int a = 1;

Python objects, names, and bindings

Boxa now has an integer 1.

Replace the contents of the box by assigning a value to the same variable:
a =2;

Python objects, names, and bindings

Now boxaPut an integer in 2

Assign one variable to another variable, copy the value of the variable, and put it in the new box:
int b = a;

Python objects, names, and bindings

Python objects, names, and bindings

##b is the second box containing the integer 2 copy. Box a has a separate copy.

2.2 Python has names

In Python, a name or identifier is like binding a label to an object.
a = 1

Python objects, names, and bindings

这里,整数对象1有一个叫做a的标签。

如果重新给a分配值,只是简单的将标签移动到另一个对象:
a = 2

Python objects, names, and bindings
Python objects, names, and bindings

现在名字a贴到了整数对象2上面。原来的整数对象1不再拥有标签a,或许它还存在,但是不能通过标签a访问它了(当对象没有任何引用时,会被回收。)

如果将一个名字分配给另一名字,只是将另一个名字标签捆绑到存在的对象上:
b = a

Python objects, names, and bindings

名字b只是绑定到与a引用的相同对象上的第二个标签而已。

虽然在Python中普遍使用“变量”(因为“变量”是普遍术语),真正的意思是名字或者标识符。Python中的变量是值得标签,不是装值得盒子。

2.3 指针?引用?名字?

C/C++中有指针,Java中有引用,Python中的名字在一定程度上等同于指针和引用。

2.1节中其他语言的例子,也只是针对于它们的基本类型而言的,若是指针或者引用,表现也跟Python的名字一样。这也在一定程度上说明了Python面向对象贯彻得更加彻底。

2.4 名字支持的操作

可以对一个变量做什么?声明变量,使用变量,修改变量的值。名字作为Python中的一个重要概念,可以对它做的操作有:

  • 定义;名字需要先定义才能使用,与变量需要先声明一样。

  • 绑定:名字的单独存在没有意义,必须将它绑定到一个对象上。

  • Python objects, names, and bindings:名字可以重新引用另一个对象,这个操作就是Python objects, names, and bindings。

  • 引用:为什么要定义名字,目的是使用它。

3、绑定的艺术

名字以及对象,它们之间必然会发生些什么。

3.1 变量的声明

其他如C/C++Java的高级语言,变量在使用前需要声明,或者说定义。以下在Java中声明变量:


public static void main(String[] args) {        int i = 0; // 先声明,后使用
        System.out.println(i); // 使用变量i}


这样,在可以访问到变量i所在作用域的地方,既可以使用i了。还有其他声明变量的方法么?好像没有了。

3.2 名字的定义

Python中有多种定义名字的途径,如函数定义,函数名就是引用函数对象的名字;类定义,类名就是指向类对象的名字,模块定义,模块名就是引用模块对象的名字;当然,最直观的还是赋值语句。

赋值语句

官方对赋值语句做了这样的说明(地址):

Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects.

即:

赋值语句被用来将名字绑定或者Python objects, names, and bindings给值,也用来修改可变对象的属性或项

那么,我们关心的,就是赋值语句将名字和值(对象)绑定起来了。

看一个简单的赋值语句:


a = 9


Python在处理这条语句时:

  1. 首先在内存中创建一个对象,表示整数9:

Python objects, names, and bindings

  1. 然后创建名字a,并把它指向上述对象:

Python objects, names, and bindings

上述过程就是通过赋值语句的名字对象绑定了。名字首次和对象绑定后,这个名字就定义在当前命名空间了,以后,在能访问到这个命名空间的作用域中可以引用该名字了。

3.3 引用不可变对象

定义完名字之后,就可以使用名字了,名字的使用称为“引用名字”。当名字指向可变对象和不可变对象时,使用名字会有不同的表现。


a = 9       #1a = a + 1   #2


语句1执行完后,名字a指向表示整数9的对象:

Python objects, names, and bindings

由于整数是不可变对象,所以在语句2处引用名字a,试图将表示整数9的对象 + 1,但该对象的值是无法改变的。因此就将该对象表示的整数值91,以整数10新建一个整数对象:

Python objects, names, and bindings

接下来,将名字a Python objects, names, and bindings 到新建对象上,并移除名字对原对象的引用:

Python objects, names, and bindings

使用id()函数,可以看到名字a指向的对象地址确实发生了改变:


>>> a = 9>>> id(a)1470514960>>> a = a + 1>>> id(a)1470514976


3.4 引用可变对象
3.4.1 示例1:改变可变对象的值

可变对象可以改变其值,并且不会造成地址的改变:


>>> list1 = [1]>>> id(list1)42695136>>> list1.append(2)>>> id(list1)42695136>>> list1
[1, 2]>>>


执行语句list1 = [1],创建一个list对象,并且其值集中添加1,将名字list1指向该对象:

Python objects, names, and bindings

执行语句list1.append(2),由于list是可变对象,可以直接在其值集中添加2

Python objects, names, and bindings

值得改变并没有造成list1引用的对象地址的改变。

3.4.2 示例2:可变对象Python objects, names, and bindings

再来看一个比较“奇怪”的例子:


values = [1, 2, 3]
values[1] = valuesprint(values)


一眼望去,期待的结果应该是


[1, [1, 2, 3], 3]


但实际上结果是:


[1, [...], 3]


我们知道list中的元素可以是各种类型的,list类型是可以的:

Python objects, names, and bindings

3.4.3 示例3:Python objects, names, and bindings可变对象

观察以下代码段:


>>> list1 = [1]>>> id(list1)42695136>>> list1 = [1, 2]>>> id(list1)42717432


两次输出的名字list1引用对象的地址不一样,这是因为第二次语句list 1 = [1, 2] 对名字做了Python objects, names, and bindings:

Python objects, names, and bindings

3.5 共享对象

当两个或两个以上的名字引用同一个对象时,我们称这些名字共享对象。共享的对象可变性不同时,表现会出现差异。

3.5.1 共享不可变对象

函数attempt_change_immutable将参数i的值修改为2


def attempt_change_immutable(i):
    i = 2i = 1print(i)
attempt_change_immutable(i)print(i)


Output:


11


如果你对输出不感到意外,说明不是新手了 ^_^。

  1. 首先,函数的参数i与全局名字i不是在同一命名空间中,所以它们之间不相互影响。

  2. 调用函数时,将两个名字i都指向了同一个整数对象。

  3. 函数中修改i的值为2, 因为整数对象不可变,所以新建值为2的整数对象,并把函数中的名字i绑定到对象上。

  4. 全局名字i的绑定关系并没有被改变。

Python objects, names, and bindings

Python objects, names, and bindings

值得注意的是,这部分内容与命名空间和作用域有关系,另外有文章介绍它们,可以参考。

3.5.2 Python objects, names, and bindings

函数attempt_change_mutable为列表增加字符串。


def attempt_change_mutable(list_param):
    list_param.append(&#39;test&#39;)

list1 = [1]print(list1)
attempt_change_mutable(list1)print(list1)


output:


[1]
[1, &#39;test&#39;]


可以看到函数成功改变了列表list1的值。传递参数时,名字list_param引用了与名字list1相同的对象,这个对象是可变的,在函数中成功修改了对象的值。

首先,名字list_param与名字list1指向对象:

Python objects, names, and bindings

然后,通过名字list_param修改了对象的值:

Python objects, names, and bindings

最后,这个修改对名字list1可见。

3.6 绑定何时发生

总的来说,触发名字对象绑定的行为有以下一些:

  • 赋值操作;a = 1

  • 函数定义;

    def test():
        pass

    将名字test绑定到函数对象

  • 类定义:

    class Test(object):
        pass

    将名字Test绑定到类对象

  • 函数传参;

    def test(i):
        pass
    test(1)

    将名字i绑定到整数对象1

  • import语句:

    import sys

    将名字sys绑定到指定模块对象。

  • <a href="http://www.php.cn/wiki/125.html" target="_blank">for</a>循环

    for i in range(10):
        pass

    每次循环都会绑定/Python objects, names, and bindings名字i

  • as操作符

    with open('dir', 'r') as f:
        pass
    
    try:
        pass
    except NameError as ne:
        pass

    with open语句,异常捕获语句中的as都会发生名字的绑定

4、其他说明

待续。。。

参考

  1. The Python Language References#Data model# Objects, values, types

  2. Python的名字绑定

  3. Python一切皆对象

  4. Code Like a Pythonista: Idiomatic Python

  5. python基础(5):深入理解 python 中的赋值、引用、拷贝、作用域

脚注

[1] 在特定的控制条件下,改变对象的类型是可能的。但不是一种明智的做法,如果处理不当的话,会发生一些奇怪的行为。

The above is the detailed content of Python objects, names, and bindings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Django Model operationNext article:Django Model operation