Most importantly, since := is an expression, it can be used in contexts ) which shouldĬlearly be equivalent to the dict comprehension.)ĭifferences between assignment expressions and assignment statements > f ' this isĪlready the case, and also in dict((X, Y) for. Repeated a subexpression, slowing down the program, in order to save Guido van Rossum searched through aĭropbox code base and discovered some evidence that programmers valueĬase in point: Guido found several examples where a programmer Have allowed a modest but clear improvement in quite a few bits ofĪnother use of real code is to observe indirectly how much value His conclusion: the current proposal would View) would have been clearer if rewritten with (sparing) use ofĪssignment expressions. Repository and picking examples of code he had written that (in his Has been extremely helpful by going over his own personal code code that was written without any thought of this PEP,Īs part of a useful application, however large or small. However, in order to be compelling, examples should be rooted in Therefore, there will be some of each below. Yet there is some use for both extremely simple and extremely complexĮxamples: they are helpful to clarify the intended semantics. The danger of overly complex examples is that they provide aĬonvenient strawman for critics of the proposal to shoot down (“that’s With “I would never write it that way anyway”. Make anyone go “ooh, that’s compelling”, and they are easily refuted The danger of toy examples is twofold: they are often too abstract to The importance of real codeĭuring the development of this PEP many people (supporters and criticsīoth) have had a tendency to focus on toy examples on the one hand,Īnd on overly complex examples on the other. Heisenbugs), and is easier to dictate to another programmer. The code be inadvertently changed as part of debugging (a common cause of Removing the need to refactor reduces the likelihood that Without a way toĬapture sub-expressions inline, this would require refactoring of the originalĬode with assignment expressions, this merely requires the insertion of a few Statement form, making it unavailable in list comprehensions and otherĪdditionally, naming sub-parts of a large expression can assist an interactiveĭebugger, providing useful display hooks and partial results. Currently, this feature is available only in Naming the result of an expression is an important part of programming,Īllowing a descriptive name to be used in place of a longer expression,Īnd permitting reuse. the CPython reference implementation uses that name internally). (as per the PEP title), but they may also be referred to as “Named Expressions” The construct’s formal name is “Assignment Expressions” This is a proposal for creating a way to assign to variables within anĮxpression using the notation NAME := expr.Īs part of this change, there is also an update to dictionary comprehensionĮvaluation order to ensure key expressions are executed before valueĮxpressions (allowing the key to be bound to a name and then re-used as part ofĭuring discussion of this PEP, the operator became informally known as Appendix C: No Changes to Scope Semantics.Appendix B: Rough code translations for comprehensions.Why not use a sublocal scope and prevent namespace pollution?.With assignment expressions, why bother with assignment statements?.Why not just turn existing assignment into an expression?.Changing the scope rules for comprehensions.Examples from the Python standard library.Specification changes during implementation.Differences between assignment expressions and assignment statements.Guido van Rossum Status : Final Type : Standards Track Created : 2 Python-Version : 3.8 Post-History : 2, 0, 2, 0, 1, Toggle light / dark / auto colour theme PEP 572 – Assignment Expressions Author : Chris Angelico, Tim Peters , PEP 572 – Assignment Expressions | Following system colour scheme Selected dark colour scheme Selected light colour scheme Python Enhancement Proposals
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |