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

Guido van Rossum guido at python.org
Fri Aug 5 12:12:23 EDT 2016


On Fri, Aug 5, 2016 at 12:40 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> I'm mostly on board with the proposal now, but have one last niggle:
> What do you think of associating the "class" more with the variable
> name than the type definition for class declarations by putting to the
> left of the colon?
>
> That is:
>
>     class Starship:
>         stats class: Dict[str, int] = {}  # Pure class variable
>         damage class: int = 0  # Hybrid class/instance variable
>         captain: str  # Pure instance variable
>
> Pronounced as:
>
>     "stats is declared on the class as a dict mapping from strings to
> integers and is initialised as an empty dict"
>     "damage is declared on the class as an integer and is initialised as zero"
>     "captain is declared on instances as an integer"
>
> Just a minor thing, but the closer association with the name reads
> better to me since "Class attribute or instance attribute?" is really
> a property of the name binding, rather than of the permitted types
> that can be bound to that name

Hmm... But the type is *also* a property of the name binding. And I
think the "class-var-ness" needs to be preserved in the
__annotations__ dict somehow, so that's another reason why it
"belongs" to the type rather than to the name binding (a nebulous
concept to begin with).

Also, I like the idea that everything between the ':' and the '=' (or
the end of the line) belongs to the type checker. I expect that'll be
easier for people who aren't interested in the type checker.

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


More information about the Python-ideas mailing list