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

Guido van Rossum guido at python.org
Mon Aug 8 16:25:33 EDT 2016


That's a very interesting idea and one that deserves pursuing (though I
agree it's not a blocker for the PEP I'm hoping to write). I think the next
step is to prototype this -- which can only happen once we have an
implementation of the PEP. Though perhaps you could start by writing a
prototype that works by having the user write the following:

class Starship(PrototypeNamedTuple):
    damage = 0
    captain = "Kirk"
    __annotations__ = dict(damage=int, captain=str)

It could also benefit from PEP 520 (Preserving Class Attribute Definition
Order).

Who's game?

--Guido

On Mon, Aug 8, 2016 at 1:13 PM, אלעזר <elazarg at gmail.com> 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 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160808/325d80ce/attachment-0001.html>


More information about the Python-ideas mailing list