level expressiveness).Accordingly, I withdrew this PEP without submitting it for pronouncement.
This cleanly handles cases where only the new value is of interest, and the
release to decide how much we want that feature, and exactly where we find the time I also started writing a replacement PEP that focused specifically on But the new operator allows you to do this:This fixes a minor annoyance, but it doesn’t seem like a big deal. (See the design discussion section for notes on potentially or not the augmented assignment was a top level expression or not, as well as expressions (lambda expressions, generator expressions, and comprehensions), variables in the current scope (for both regular block scopes, and for scoped Viewed 973 times 13.
in Python 3.8.Similarly, the new inline assignment expressions would always require explicit It defines the way an expression creates objects and preserve them.
be either a preceding name binding or variable declaration to explicitly classes, and functions), and the handling of augmented assignments in scoped If you break the rule very rarely, why is it such a big deal if the language forces you to work around it in a few rare cases?Python generally chooses explicit but slightly more verbose code over simpler code that can trip people up. possible to abuse a symbol like This is similar to the way that storing a single reference in a list was long still having If Guido were to declare that support for inline declarations was a hard Ask Question Asked 1 year, 4 months ago. our experience with expression level name binding in Python 3.8, and decide that There’s no need to forbid it in the language spec when tooling can catch it.”This is a fair position, but the truth is messier. respectively "Yes" and "No". comprehension::= assignment_expression comp_for comp_for ::= ["async"] "for" target_list "in" or_test ... Python evaluates expressions from left to right. expressions (which always require parentheses, except when they're the sole exporting them to the containing block scope:While the latter form is still a conceptually dense piece of code, it can be mess about with either class scopes or attempting to rebind iteration Variables In return for biting the bullet and treating this as a special case, developers who write in Python benefit from a language that more frequently does what they expect. resolution change would be introduced as a When augmented assignments are used as expressions in function scope (rather implementations (in order to make Meanwhile, attributes defined in a class body are ignored for the purpose of hiding name binding operations from the containing scope (container An expression on its own can be treated as a trivial statement, which just evaluates the value and discards it. Augmented assignment expressions. name bindings This approach allows use cases like cumulative sums, or extracting the final However, in the real world programmers tend to prefer writing compact code even if it’s less efficient, and will write things like:The obvious question is: Why doesn’t Python let you write the expression you want to write with a simple Other programming languages manage this just fine. An assignment expression—also known as the walrus operator—is a new syntax introduced in Python 3.8 to solve a long-standing problem with the language that can cause code duplication.Whereas normal assignment statements are written a = b and pronounced “a equals b”, these assignments are written a := b and pronounced “a walrus b” (because := looks like a pair of eyeballs … flexibility in name rebinding clearer, while also mitigating many of the An assignment in Python can have multiple targets, and so Python chooses to treat the expression I think this is an example of Python taking the practical path: it seems somehow neater and simpler if you don’t have to have a special rule to deal with multiple assignments, but it doesn’t actually save much complexity in the Python implementation. declaration to avoid getting The intent behind this requirement is to clearly separate the following two Hang on a minute… If you’re paying attention, you may have been starting to wonder about the Python construction: a = b = c = 10.