这些文章来自《Python cookbook》.

翻译这些文章仅仅是我为了学习使用。

从第一章第二节开始,

因为第一节是介绍。以后有空的话,把它补上。

个人认为这本书很适合已经入门的python程序员来提高。请多多指教。

1.2 Swapping Values WithoutUsing a Temporary Variable

不使用临时变量来交换值

Credit: Hamish Lawson

1.2.1 Problem

问题

You want to swap the values of some variables, but you don't want to use a temporary variable.

你想去交换一些变量的值,但是你不想使用临时变量。

1.2.2 Solution

解决

Python's automatic tuple packing and unpacking make this a snap:

python的自动tuple打包和解包可以做到这点:

a, b, c = b, c, a

1.2.3 Discussion

讨论

Most programming languages make you use temporary intermediate variables to swap variable values:

许多程序语言让你使用临时中间变量去交换变量的值。

temp = a
a = b
b = c
c = temp

But Python lets you use tuple packing and unpacking to do a direct assignment:

但是python让你使用tuple的打包和解包来直接赋值

a, b, c = b, c, a

In an assignment, Python requires an expression on the righthand side of the =. What we wrote there?TT>b, c, a梚s indeed an expression. Specifically, it is a tuple, which is an immutable sequence of three values. Tuples are often surrounded with parentheses, as in (b, c, a), but the parentheses are not necessary, except where the commas would otherwise have some other meaning (e.g., in a function call). The commas are what create a tuple, by packing the values that are the tuple's items.

在一个赋值中,python要求等号右边是一个表达式。 这里我们所写的b, c, a的确是表达式。要明确的是,它是一个tuple, 有三个元素的不可变的序列。Tuple经常用圆括号括起来, 象(b, c, a)。 但是在这里除了逗号是有一些其他的意思以外(在一个函数调用中),圆括号不是必须的。通过打包tuple的每一项,用逗号来建立一个tuple.

On the lefthand side of the = in an assignment statement, you normally use a single target. The target can be a simple identifier (also known as a variable), an indexing (such as alist[i] or adict['freep']), an attribute reference (such as anobject.someattribute), and so on. However, Python also lets you use several targets (variables, indexings, etc.), separated by commas, on an assignment's lefthand side. Such a multiple assignment is also called an unpacking assignment. When there are two or more comma-separated targets on the lefthand side of an assignment, the value of the righthand side must be a sequence of as many items as there are comma-separated targets on the lefthand side. Each item of the sequence is assigned to the corresponding target, in order, from left to right.

在赋值语句的等号的左边,你可以正常的使用单个的对象。 这个对象能够是简单的一个标示符号(也就是众所周知的变量), 一个索引(例如alist[i] 和 adict['freep']), 一个属性的引用(如anobject.someattribute)等等。然而,python也让你在赋值语句的左边,使用几个用逗号分开的对象(变量,索引等等)。 那样的一个多赋值也被称为解包赋值。 当有两个或者更多的逗号分隔开的对象在一个赋值语句的左边, 右边的值必须是一个序列,而且它的数目要和左边逗号分隔的对象一样多。序列的每一项都被从左到右依次赋给相对应的目标值。

In this recipe, we have three comma-separated targets on the lefthand side, so we need a three-item sequence on the righthand side, the three-item tuple that the packing built. The first target (variable a) gets the value of the first item (which used to be the value of variable b), the second target (b) gets the value of the second item (which used to be the value of c), and the third and last target (c) gets the value of the third and last item (which used to be the value of a). The net result is a swapping of values between the variables (equivalently, you could visualize this particular example as a rotation).

在这个配方中(译注:书的名字叫python食谱,所以每一节都称为一个配方:)), 我们有三个逗号分隔的目标对象在左边,所以我们需要右边有三个元素的序列,他们是有三个元素的被打包的tuple。第一个目标(变量)得到第一个元素的值(它使用变量b的值), 第二个目标(b)得到第二个元素的值(它使用变量c的值), 并且第三个即最后一个目标(c)得到第三即最后一个元素(使用变量a的值)。 实际结果是一个在变量之间交换的值(你可以想想这个例子细节等价于一个旋转)

Tuple packing, done using commas, and sequence unpacking, done by placing several comma-separated targets on the lefthand side of a statement, are both useful, simple, general mechanisms. By combining them, you can simply, elegantly, and naturally express any permutation of values among a set of variables.

Tuple使用逗号打包,序列通过放置几个逗号分隔的目标到语句的左边来解包。他们都是有用的,简单的,普遍的机制。通过结合他们,你能简单地,优美地,自然地表达任何一组变量之间的值交换

1.2.4 See Also

参考

The Reference Manual section on assignment statements.

参考手册的赋值语句部分


上一页 目录 下一页

PyCkBk-1-2 (last edited 2009-12-25 07:15:35 by localhost)