[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484

Paul Moore p.f.moore at gmail.com
Tue Aug 2 03:54:24 EDT 2016


On 2 August 2016 at 00:41, Ethan Furman <ethan at stoneleaf.us> wrote:
> As far as questions such as "what would this do" I would say it should do
> the
> exact same thing as if the type info wasn't there:
>
>   a: int, b: str = x   simplifies to
>
>   a, b = x
>
> and so x had better be a two-item iterable

For me,

   a: int, b: str = x

immediately says "leave a unassigned, and assign x to b". Maybe that's
my C experience at work.

But

    a, b = x

immediately says tuple unpacking.

So in my view, allowing annotations on unpacking syntax is going to
cause a *lot* of confusion, and I'd strongly argue for only allowing
type annotations on single variables:

    VAR [: TYPE] [= INITIAL_VALUE]

For multiple variables, just use multiple lines:

    a: int
    b: str = x

or

    a: int
    b: str
    a, b = x

depending on your intention.

>From what Guido says, the main use case is class variables, where
complicated initialisations are extremely rare, so this shouldn't be a
problem in practice.

Paul


More information about the Python-ideas mailing list