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

Steven D'Aprano steve at pearwood.info
Thu Apr 26 20:36:05 EDT 2018


Hi Fatty, and welcome!

On Thu, Apr 26, 2018 at 08:00:55PM +0200, Fatty Morgan wrote:

> 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)'.

I'm not sure why you say that is the "natural" interpretation, 
unless you're saying that Guido, Chris, myself and dozens of other 
people taking part of this conversation are unnatural, since none of us 
thought of that interpretation *smiles*

The := token is the second most common assignment operator in 
programming languages, behind only = single equals sign. For those of us 
who were raised on Pascal, it is entirely natural to use = for equality 
tests and := for assignment, and languages that use == for equality are 
the ones which are weird.

Since type-annotations are still only used by a small proportion of 
Python code and Python developers, I doubt that they will jump to the 
interpretation of "explicit type hint with no type given".

If the type-checker can infer the type of the expression, there's no 
need to use the colon at all.

    name := expression  # can infer type here
    name = expression  # why not just infer the type here?

So using : Type without the type is entirely unnecessary. The colon is 
only needed when you have to specify a type manually.

Your comments about some entirely hypothetical "Python with enforced 
static typing" are interesting but so blue-sky that I honestly doubt 
that there's any point in discussing them now. We're focused on Python 
3.8 and 3.9, not Python 5 or Python 6.


-- 
Steve


More information about the Python-Dev mailing list