TLDR: I Changed the Rules for the Python's Walrus Operator
Date: 2021-04-01 Source: https://arpitbhayani.me/blogs/the-weird-walrus
Overview
Explore Python's walrus operator ( -=) and its quirks! Learn why a -= 10 throws an error and how to modify CPython to allow it. Python in version 3.8 introduced Assignment Expressions which can be used with the help of the Walrus Operator :=.
Key Points
- The Walrus Operator or Assignment Expressions are called Named Expressions in CPython.
- The branch of the CPython we are referring to here is for version 3.8
- The Grammar: If a := 10 is giving us a Syntax Error then it must be linked to the Grammar specification of the language.
- What about
ifand ?: According to the rule if_stmt and while_stmt you can have named expressions right after if without needing any brackets surrounding it.