Some Thoughts on the exchange value of the variable in python

In programming, once referred to the exchange value of the variable, the first to come to mind is the practice of introducing a temporary variable as a medium to do, take a look at the implementation.

solution

Assume that there are two variables x, y, as follows:

x = 10
y = 20
复制代码

Common scheme, a temporary variable is defined as a medium to achieve the exchange value of the variable. To achieve the following:

t = x
x = y
y = t
复制代码

pythonic, in fact, this need for python provides a more convenient solution for us.

x, y = y, x
复制代码

Code from the intuitive understanding intended herein, i.e., to achieve the exchange value of the variable x and y. Here are very easy to understand, but then we need to think about: how this wording performance? Why it can so easily is to realize the value of the variable exchange?

Performance Comparison

Although the wording simple and convenient, but whether the loss of performance for the price of it? It defines two functions:

def swap1():
    x = 1
    y = 2
    t = x 
    x = y
    y = t

def swap2():
    x = 1
    y = 2
    x, y = y, x
复制代码

In order to better show the performance, two cycle call function calls 100, respectively (needs to be performed in ipython):

swap1 consuming 38μs

%time a = [swap1() for _ in range(100)]
复制代码

The results are as follows:

CPU times: user 31 µs, sys: 7 µs, total: 38 µs
Wall time: 67 µs
复制代码

swap2 consuming 18μs

%time a = [swap2() for _ in range(100)]
复制代码

The results are as follows:

CPU times: user 18 µs, sys: 0 ns, total: 18 µs
Wall time: 21 µs
复制代码

It can be seen pythonic wording is much faster than the new auxiliary variables introduced simple and crude. Written so simple and high performance, why not do it.

This article python interview value of the exchange value of the variable , from the bottom to explain the reasons for differences in performance in two ways. swap2 value variable by exchange of two commands ROT_TWO LOAD_FAST + definitely higher than the two instructions executed STORE_FAST efficiency.

Note: For switching two variables, in fact, did not use a tuple, you can look at this video

More than some think

Rethinking then the following question: Why python can be written in this way to assign it?

See some of the expression on the right of the assignment operator, i.e. y, x, which is actually a data structure called tuples in python. We can see the left side of an assignment expression is x, y, then why tuple can be assigned directly to the x, y it?

Here the use of a characteristic of the python, i.e. any sequence (or iterative object) can be decomposed into separate variable by a simple assignment. Let us look at an example:

name, age, mobile = 'polo', 30, '15312210823'
复制代码

This code can execute the name assigned to polo, age assigned to 30, phone assigned to 15,312,210,823.

Extension extension

In addition to this simple dismantling of the above sequence, Python also supports other more complex scenarios, look at the following nested exploded variables, the most intuitive example:

school_name, (student_name, stduent_age, stduent_sex) = '致远中学', ('polo', 18, 'M')
复制代码

Decomposition can also support flexible variable length sequence, for example, now we have a list of accomplishments class students ordered as follows:

scores = [21, 34, 36, 56, 60, 75, 76, 81, 83, 86, 86, 89, 90, 95, 98, 99]
复制代码

We aim to get the maximum score, minimum score, and a list of other students, you can quickly get direct data required by the explosion sequence:

min_score, *other_scores, max_score = scores
复制代码

Here introduces a new wording, variable * expression easily break out in the middle of iterable and assigned to other_scores, at the same time were assigned at the beginning and end of the object to the min_score and max_score.

See here seems a bit like the feeling of a sequence decomposition regular expression pattern matching.

to sum up

Although only a small exchange value of the variable, but the essence is determined by the needs and characteristics of the language itself. Learn the necessary skills, will help us to write higher quality code.

Reproduced in: https: //juejin.im/post/5cee0648f265da1bd3053b9c

Guess you like

Origin blog.csdn.net/weixin_34112208/article/details/91428706