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

Guido van Rossum guido at python.org
Mon Aug 1 19:41:16 EDT 2016


On Mon, Aug 1, 2016 at 3:40 PM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> On Mon, Aug 1, 2016 at 3:31 PM, Guido van Rossum <guido at python.org> wrote:
>> (I've considered a 'var' keyword in the past, but there just are too
>> many variables named 'var' in my code. :-)
>
> As noted by someone else, what about "local":
>
> local a: float

A common use of `# type:` is in class bodies. But `local` is a bad idea there.

> It seems like nonlocal and global would need the same treatment:
>
> nonlocal b: List[int]
> global c: Optional[str]

Well, they don't support assignment either, so I'd like to keep them
out of this if possible.

>> There are some corner cases to consider. First, to declare a
>> variable's type without giving it an initial value, we can write this:
>>
>> a: float
>
> Isn't that currently a NameError?  Is it worth making this work while
> preserving the error case?  A "local" keyword would solve the problem,
> no?

Currently looks like a syntax error to me.

>> Second, when these occur in a class body, they can define either class
>> variables or instance variables. Do we need to be able to specify
>> which?
>
> In the immediate case I'd expect the former.  We don't currently have
> a canonical way to "declare" instance attributes in the class
> definition.  It may be worth sorting that out separately and
> addressing the PEP 484 aspect at that point.

My observation is that most uses of `# type: ...` in class bodies is
used to declare an instance variable.

>> Third, there's an annoying thing with tuples/commas here.
>
> What about unpacking into explicit displays?  For example:
>
> (a, b) = 0, ''
> [a, b] = 0, ''
>
> -eric

Yeah.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list