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

Nick Coghlan ncoghlan at gmail.com
Fri Aug 5 03:40:04 EDT 2016


On 5 August 2016 at 02:40, Guido van Rossum <guido at python.org> wrote:
> class Starship:
>     stats: class Dict[str, int] = {}  # Pure class variable
>     damage: class int = 0  # Hybrid class/instance variable
>     captain: str  # Pure instance variable

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

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list