Home > Article > Backend Development > Python objects, names, and bindings
Unless otherwise specified, the following are Based on
Python3
Python
philosophy:
Everything in Python is Object
Object is Python
's abstraction of data. Python
All 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. is
OperatorCompares 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
tracing function may cause objects that should be recycled to remain alive. Use try...
except
The 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
Method. It is strongly recommended to release resources explicitly in the program. The try
...
final
ly<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.
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; CPython
Explanation 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; is
Compares 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'a test module'author = 'Richard Cheng'import sysclass Person(object): ''' Person class''' def init(self, name, age): self.name = name self.age = agedef tset(): print(sys.path) p = Person('Richard', 20) print(p.name, ':', p.age)def main(): tset()if name == 'main': main()
This paragraph Python
There are many objects in the code, including the hello
module object, the created Person
class object, and several functions such as test
, main
Function objects, numbers, strings, and even the code itself are also objects.
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"
In other languages, assigning values to variables is like The value is placed in the "box". int a = 1;
Boxa
now has an integer 1
.
Replace the contents of the box by assigning a value to the same variable: a =2;
Now boxa
Put 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;
##b is the second box containing the integer 2 copy. Box
a has a separate copy.
In Python, a name or identifier is like binding a label to an object.
a = 1
这里,整数对象1
有一个叫做a
的标签。
如果重新给a
分配值,只是简单的将标签移动到另一个对象:a = 2
现在名字a
贴到了整数对象2
上面。原来的整数对象1不再拥有标签a
,或许它还存在,但是不能通过标签a
访问它了(当对象没有任何引用时,会被回收。)
如果将一个名字分配给另一名字,只是将另一个名字标签捆绑到存在的对象上:b = a
名字b
只是绑定到与a
引用的相同对象上的第二个标签而已。
虽然在Python
中普遍使用“变量”(因为“变量”是普遍术语),真正的意思是名字或者标识符。Python
中的变量是值得标签,不是装值得盒子。
C/C++
中有指针,Java
中有引用,Python
中的名字在一定程度上等同于指针和引用。
2.1节中其他语言的例子,也只是针对于它们的基本类型而言的,若是指针或者引用,表现也跟Python
的名字一样。这也在一定程度上说明了Python
将面向对象贯彻得更加彻底。
可以对一个变量做什么?声明变量,使用变量,修改变量的值。名字作为Python
中的一个重要概念,可以对它做的操作有:
定义;名字需要先定义才能使用,与变量需要先声明一样。
绑定:名字的单独存在没有意义,必须将它绑定到一个对象上。
Python objects, names, and bindings:名字可以重新引用另一个对象,这个操作就是Python objects, names, and bindings。
引用:为什么要定义名字,目的是使用它。
名字以及对象,它们之间必然会发生些什么。
其他如C/C++
和Java
的高级语言,变量在使用前需要声明,或者说定义。以下在Java
中声明变量:
public static void main(String[] args) { int i = 0; // 先声明,后使用 System.out.println(i); // 使用变量i}
这样,在可以访问到变量i
所在作用域的地方,既可以使用i
了。还有其他声明变量的方法么?好像没有了。
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
在处理这条语句时:
首先在内存中创建一个对象,表示整数9
:
然后创建名字a
,并把它指向上述对象:
上述过程就是通过赋值语句的名字对象绑定了。名字首次和对象绑定后,这个名字就定义在当前命名空间了,以后,在能访问到这个命名空间的作用域中可以引用该名字了。
定义完名字之后,就可以使用名字了,名字的使用称为“引用名字”。当名字指向可变对象和不可变对象时,使用名字会有不同的表现。
a = 9 #1a = a + 1 #2
语句1执行完后,名字a
指向表示整数9
的对象:
由于整数是不可变对象,所以在语句2处引用名字a
,试图将表示整数9
的对象 + 1
,但该对象的值是无法改变的。因此就将该对象表示的整数值9
加1
,以整数10
新建一个整数对象:
接下来,将名字a
Python objects, names, and bindings
到新建对象上,并移除名字对原对象的引用:
使用id()
函数,可以看到名字a
指向的对象地址确实发生了改变:
>>> a = 9>>> id(a)1470514960>>> a = a + 1>>> id(a)1470514976
可变对象可以改变其值,并且不会造成地址的改变:
>>> list1 = [1]>>> id(list1)42695136>>> list1.append(2)>>> id(list1)42695136>>> list1 [1, 2]>>>
执行语句list1 = [1]
,创建一个list
对象,并且其值集中添加1
,将名字list1
指向该对象:
执行语句list1.append(2)
,由于list
是可变对象,可以直接在其值集中添加2
:
值得改变并没有造成list1
引用的对象地址的改变。
再来看一个比较“奇怪”的例子:
values = [1, 2, 3] values[1] = valuesprint(values)
一眼望去,期待的结果应该是
[1, [1, 2, 3], 3]
但实际上结果是:
[1, [...], 3]
我们知道list
中的元素可以是各种类型的,list
类型是可以的:
观察以下代码段:
>>> list1 = [1]>>> id(list1)42695136>>> list1 = [1, 2]>>> id(list1)42717432
两次输出的名字list1
引用对象的地址不一样,这是因为第二次语句list 1 = [1, 2]
对名字做了Python objects, names, and bindings:
当两个或两个以上的名字引用同一个对象时,我们称这些名字共享对象。共享的对象可变性不同时,表现会出现差异。
函数attempt_change_immutable
将参数i
的值修改为2
def attempt_change_immutable(i): i = 2i = 1print(i) attempt_change_immutable(i)print(i)
Output:
11
如果你对输出不感到意外,说明不是新手了 ^_^。
首先,函数的参数i
与全局名字i
不是在同一命名空间中,所以它们之间不相互影响。
调用函数时,将两个名字i
都指向了同一个整数对象。
函数中修改i
的值为2
, 因为整数对象不可变,所以新建值为2
的整数对象,并把函数中的名字i
绑定到对象上。
全局名字i
的绑定关系并没有被改变。
值得注意的是,这部分内容与命名空间和作用域有关系,另外有文章介绍它们,可以参考。
函数attempt_change_mutable
为列表增加字符串。
def attempt_change_mutable(list_param): list_param.append('test') list1 = [1]print(list1) attempt_change_mutable(list1)print(list1)
output:
[1] [1, 'test']
可以看到函数成功改变了列表list1
的值。传递参数时,名字list_param
引用了与名字list1
相同的对象,这个对象是可变的,在函数中成功修改了对象的值。
首先,名字list_param
与名字list1
指向对象:
然后,通过名字list_param
修改了对象的值:
最后,这个修改对名字list1
可见。
总的来说,触发名字对象绑定的行为有以下一些:
赋值操作;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
都会发生名字的绑定
待续。。。
The Python Language References#Data model# Objects, values, types
Python的名字绑定
Python一切皆对象
Code Like a Pythonista: Idiomatic Python
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!