[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484
אלעזר
elazarg at gmail.com
Tue Aug 9 17:47:19 EDT 2016
It's already possible to overload NamedTuple, in a way that will allow the
following abuse of notation:
@NamedTuple
def Starship(damage:int, captain:str): pass
The 'def' is unfortunate and potentially confusing (although it *is* a
callable definition), and the ": pass" is meaningless. But I think it is
clear and concise if you know what NamedTuple is.
Introducing new keyword will of course solve both problems (if there's
"async def", why not "type def"? :) ).
case class Starship(damage:int, captain:str)
Possible variations on the decorator theme:
"unordered" namedtuple (note the *)
@Value
def Starship(*, damage:int, captain:str): pass
self-describing (another level of notation abuse):
@Type
def Starship(damage:int, captain:str) -> NamedTuple: pass
On Mon, Aug 8, 2016 at 11:25 PM Guido van Rossum <guido at python.org> wrote:
> 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/20160809/06a8b96a/attachment-0001.html>
More information about the Python-ideas
mailing list