[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484
Neil Girdhar
mistersheik at gmail.com
Wed Aug 10 00:46:01 EDT 2016
With PEP 520 accepted, would it be possible to iterate over
__definition_order__?
class PrototypeNamedTuple:
cache = {}
def __new__(cls, *args):
P = PrototypeNamedTuple
if cls not in P.cache:
P.cache[cls] = typing.NamedTuple(cls.__name__,
[(definition, cls.__annotations__[definition])
for definition in cls.__definition_order__]
)
return P.cache[cls](*args)
On Monday, August 8, 2016 at 5:09:50 PM UTC-4, Guido van Rossum wrote:
>
> Hm, overlooking the ordering is kind of a big deal for something with
> "tuple" in its name. :-)
>
> Also it feels like it needs a metaclass instead of a cache.
>
> Maybe from this we can learn though that __anotations__ should be an
> OrderedDict?
>
> On Mon, Aug 8, 2016 at 1:58 PM, אלעזר <ela... at gmail.com <javascript:>>
> wrote:
>
>> class PrototypeNamedTuple:
>> cache = {}
>> def __new__(cls, *args):
>> P = PrototypeNamedTuple
>> if cls not in P.cache:
>> P.cache[cls] = typing.NamedTuple(cls.__name__,
>> cls.__annotations__.items())
>> return P.cache[cls](*args)
>>
>> Works modulo ordering, though I'm not sure that's the right way to do it.
>>
>> The ordering part of namedtuple is orthogonal to the
>> value-type/immutability part. So I would imagine making "Value" for the
>> latter, "tuple" for the former, and namedtuple is mixing both (possibly
>> given a convenient name, such as PrototypeNamedTuple). "Value" can also
>> seen as mixing "Struct" and "Immutable", but that's overdoing it I guess.
>>
>> ~Elazar
>>
>> On Mon, Aug 8, 2016 at 11:25 PM Guido van Rossum <gu... at python.org
>> <javascript:>> 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, אלעזר <ela... at gmail.com <javascript:>>
>>> 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... at python.org <javascript:>
>>>> https://mail.python.org/mailman/listinfo/python-ideas
>>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>>
>>>
>>>
>>>
>>> --
>>> --Guido van Rossum (python.org/~guido)
>>>
>>
>
>
> --
> --Guido van Rossum (python.org/~guido)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160809/f53cb860/attachment-0001.html>
More information about the Python-ideas
mailing list