# Two key points of Python deconstruction and encapsulation

The following article comes from Tencent cloud, written by brother Liang in the workplace

(want to learn Python? Python learning exchange group: 1039649593, meet your needs. All materials have been uploaded to the group file stream and can be downloaded by yourself! There are also a large number of the latest 2020 Python learning materials.)

### Python deconstruction and encapsulation

Look at the following code first

```x = 1
y = 2

tmp = x
x = y
y = tmp

print(x, y)
```

The output of the code is: 2 1

Look at the following code:

```x = 1
y = 2

x, y = y, x
print(x, y)
```

The output of the code is: 2 1

x. The meaning behind the code y = y, X is deconstruction and encapsulation

### Python encapsulation

```In : t = 1, 2

In : t
Out: (1, 2)

In : type(t)
Out: tuple			# Parentheses can be omitted when defining tuples

In : t1 = (1, 2)

In : t2 = 1, 2
# t1 and t2 are equivalent
In : t1
Out: (1, 2)

In : t2
Out: (1, 2)
```

Therefore, the encapsulated result must be tuples.

x. Y = y, x the right side of the code will be encapsulated as (y, x)

### Python deconstruction

##### Basic deconstruction
```In : lst = [1, 2]

In : first, second = lst

In : print(first, second)
1 2
```

Assign the elements of the linear structure lst to the variables first and second according to the element order

##### Asterisk deconstruction
```In : lst = list(range(5))

In : head, *tail = lst

Out: 0

In : tail
Out: [1, 2, 3, 4]

In : *lst2 = lst	# There must be a variable with an asterisk on the left
File "<ipython-input-15-98211a44ccfb>", line 1
*lst2 = lst
^
SyntaxError: starred assignment target must be in a list or tuple

In : *head, tail = lst

Out: [0, 1, 2, 3]

In : lst
Out: [0, 1, 2, 3, 4]

In : tail
Out: 4

In : head, *m1, *m2, tail = lst		# There cannot be more than one asterisk. There can only be one asterisk
File "<ipython-input-20-1fc1a52caa8e>", line 1
head, *m1, *m2, tail = lst
^
SyntaxError: two starred expressions in assignment

In : v1, v2, v3, v4, v5, v6, v7 = lst	# The number of variables on the left cannot exceed the number of elements on the right
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-9366cfb498a1> in <module>()
----> 1 v1, v2, v3, v4, v5, v6, v7 = lst

ValueError: not enough values to unpack (expected 7, got 5)

In : v1, v2 = lst						#The number of variables on the left cannot be less than the number of elements on the right
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-d7b0a4e7871e> in <module>()
----> 1 v1, v2 = lst

ValueError: too many values to unpack (expected 2)

```

### It can be summarized as the following rules:

• Elements are assigned to variables in order
• Variables and elements must match
• Asterisk variables can accept any number of elements
• Variables with asterisks cannot appear alone

### Multilevel deconstruction

Deconstruction supports multiple levels

```In : lst = [1, (2, 3), 5]

In : _, v, *_ = lst		# v resolves to (2, 3)

In : v
Out: (2, 3)

In : _, val = v			# v can be further deconstructed

In : val
Out: 3

In : _, (_, val), *_ = lst		# It can be deconstructed in one step

In : val
Out: 3

In : _, [*_, val], *_ = lst		# Middle part solution composition list

In : val
Out: 3

In : _, _, val, *_ = lst		# (2, 3) parse into the second_

In : val
Out: 5
```
##### Use of Python underscores

Use single underline_ Indicates that the variable is discarded, which is a python convention. A single underscore is also a legal identifier for Python, but if you don't want to discard a variable, you usually don't use a single underscore to represent a meaningful variable. It can be understood as a convention.

##### Use of deconstruction and encapsulation

For very complex data structures and multi-layer nested linear structures, deconstruction can be used to quickly extract the values, which is very convenient

For example, the following usage methods

```In : key, _, value = 'I love Python'.partition(' love ')

In : key
Out: 'I'

In : value
Out: 'Python'
```

Tags: Python

Posted by cshinteractive on Sun, 08 May 2022 12:08:08 +0300