Python notes----deconstruction and encapsulation

Source: Internet
Author: User

Elements are refactored on the left and the elements on the right are encapsulated



Encapsulation: Defines a tuple, you can omit parentheses, the encapsulated must be a tuple

X, y = y, X


Deconstruction: Assigning elements of linear deconstruction to variables in order of elements


the deconstruction changes in Python3

You can accept all elements by using the asterisk * plus a variable

Can not use the asterisk as a single variable to receive, if you can, the equivalent of lst[0:0]

In the same deconstruction, only one asterisk can be used

When the left variable exceeds the number of elements on the right, it is not allowed


The elements must match the number of variables and the number of elements in order to be matched with an asterisk variable, you can accept any number of elements plus an asterisk variable cannot appear in Python alone, using an underscore _ _ to discard the variable

A single underline is a legitimate identifier for Python, but if you are not dropping a variable, you usually don't use a single underline to represent a meaningful variable

_, (*_, tail), *_ = LST #解构是支持多层的


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.