[Python-Dev] Reserve ':=' for type-inferred variable initialization (was PEP 572) (original) (raw)

Lukasz Langa lukasz at langa.pl
Thu Apr 26 14:28:37 EDT 2018


On Apr 26, 2018, at 11:00 AM, Fatty Morgan <famopy at gmail.com> wrote:

I would like to urge you to reconsider the use of the token ':=' for assignment expressions. The natural interpretation of 'name := expr' is a PEP 526 type-annotated variable initialization 'name : T = expr' with the type annotation T omitted, the tokens ':' and '=' coalesced, and the implied type T inferred as 'type(expr)'.

PEP 484 type checkers always infer the type of an assignment based on the right-hand side's value.

The main reason we require PEP 526 annotations at all is when your initial assignment is not useful for this inference.

The two most common examples of this are None and empty containers. In those cases the type checker is unable to infer what the intended value should be, so it urges the programmer to explicitly state it.

In this sense, a := [] or a := None doesn't provide any additional information. And a = 1 is already enough to determine what the expected type is.

The following should probably be discussed in python-ideas and would only be relevant for a more distant future, but while I have your attention, here are a couple of thoughts:

Discussing this here will add to noise for Chris and the BDFL. Consider re-posting in python-ideas.

-- Ł



More information about the Python-Dev mailing list