Since Python 3.8, the "walrus" operator (:=) has introduced assignment expressions to the language. This new feature allows programmers to assign values within comprehensions and lambda functions, which were previously restricted to regular assignments.
The primary motivation behind assignment expressions is to enable more concise and efficient code in scenarios where traditional assignments are not supported. For instance, assignment expressions facilitate:
Assignment expressions follow a specific syntax: name := expr, where name is an identifier and expr is any valid Python expression. The value of the assignment expression is the same as the expression expr, but an additional side effect assigns the value to the target name.
Assignment expressions differ from regular assignment statements in several key aspects:
Despite the withdrawal of PEP 379, which proposed a similar concept, PEP 572 introduced assignment expressions to Python for several reasons:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3