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

אלעזר elazarg at gmail.com
Mon Aug 8 16:13:47 EDT 2016


>
> class Starship:
>     stats: class Dict[str, int] = {}  # Pure class variable
>     damage: class int = 0  # Hybrid class/instance variable
>     captain: str  # Pure instance variable
>

I can't avoid noting that there is an opportunity here to insert NamedTuple
into the core language. The above example is almost there, except it's
mutable and without convenient methods. But

class Starship(tuple):
    damage: int = 0
    captain: str  = "Kirk"

Is an obvious syntax for

    Starship = NamedTuple('Starship', [('damage', int), ('captain', str)])

Only much more available and intuitive to read, use, and of course - type
check.
(Of course, it does mean adding semantics to the declaration syntax in
general)

I'm not really suggesting to make this change now, but I believe it will be
done, sooner or later. My brief experience with mypy convinced me that it
must be the case. The new declaration syntax only makes it easier.

~Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160808/3239251a/attachment.html>


More information about the Python-ideas mailing list