<div dir="ltr">With PEP 520 accepted, would it be possible to iterate over __definition_order__?<div><br></div><div><div><span style="line-height: 1.5;">class PrototypeNamedTuple:</span><br></div><div>    cache = {}</div><div>    def __new__(cls, *args):</div><div>        P = <span style="line-height: 1.5;">PrototypeNamedTuple</span></div><div>        if cls not in P.cache:</div><div>            P.cache[cls] = typing.NamedTuple(cls.__name__<wbr>, </div><div><span style="line-height: 1.5;"><br></span></div><div><span style="line-height: 1.5;">[(definition, cls.__annotations__[definition])</span></div><div><span style="line-height: 1.5;">for definition in cls.__definition_order__]</span></div><div><span style="line-height: 1.5;"><br></span></div><div><span style="line-height: 1.5;">)</span></div><div>        return P.cache[cls](*args)</div><br>On Monday, August 8, 2016 at 5:09:50 PM UTC-4, Guido van Rossum wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir="ltr"><div><div>Hm, overlooking the ordering is kind of a big deal for something with "tuple" in its name. :-)<br><br></div>Also it feels like it needs a metaclass instead of a cache.<br><br></div>Maybe from this we can learn though that __anotations__ should be an OrderedDict?<br></div><div><br><div class="gmail_quote">On Mon, Aug 8, 2016 at 1:58 PM, אלעזר <span dir="ltr"><<a href="javascript:" target="_blank" gdf-obfuscated-mailto="CwPjBcH2AQAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">ela...@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><span style="line-height:1.5">class PrototypeNamedTuple:</span><br></div><div>    cache = {}</div><div>    def __new__(cls, *args):</div><div>        P = <span style="line-height:1.5">PrototypeNamedTuple</span></div><div>        if cls not in P.cache:</div><div>            P.cache[cls] = typing.NamedTuple(cls.__name__<wbr>, <span style="line-height:1.5">cls.__annotations__.items())</span></div><div>        return P.cache[cls](*args)</div><div><br></div><div>Works modulo ordering, though I'm not sure that's the right way to do it.</div><div><br></div><div>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 <span style="line-height:1.5">PrototypeNamedTuple). "Value" can also seen as mixing "Struct" and "Immutable", but that's overdoing it I guess.</span><span style="line-height:1.5"></span></div><div><br></div>~Elazar<div><div><div><br><div class="gmail_quote"><div dir="ltr">On Mon, Aug 8, 2016 at 11:25 PM Guido van Rossum <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="CwPjBcH2AQAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">gu...@python.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div>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:<br><br></div>class Starship(PrototypeNamedTuple):<br></div>    damage = 0<br></div>    captain = "Kirk"<br></div>    __annotations__ = dict(damage=int, captain=str)<br><br></div>It could also benefit from PEP 520 (Preserving Class Attribute Definition Order).<br><br></div>Who's game?<br><br></div>--Guido<br></div><div><br><div class="gmail_quote"></div></div><div><div class="gmail_quote">On Mon, Aug 8, 2016 at 1:13 PM, אלעזר <span dir="ltr"><<a href="javascript:" target="_blank" gdf-obfuscated-mailto="CwPjBcH2AQAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">ela...@gmail.com</a>></span> wrote:<br></div></div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">class Starship:<br>
    stats: class Dict[str, int] = {}  # Pure class variable<br>
    damage: class int = 0  # Hybrid class/instance variable<br>
    captain: str  # Pure instance variable<br></blockquote><div><span style="line-height:1.5"><br></span></div></span><div><span style="line-height:1.5">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</span><br></div><div><span style="line-height:1.5"><br></span></div><div>class Starship(tuple):<br>    damage: int = 0  <br>    captain: str  = "Kirk"<span style="line-height:1.5"><br></span></div><div><br></div><div>Is an obvious syntax for </div><div><br></div><div>    <span style="line-height:1.5">Starship = </span><span style="line-height:1.5">NamedTuple('</span><span style="line-height:1.5">Starship', [('damage', int), ('captain', str)])</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">Only much more available and intuitive to read, use, and of course - type check.</span></div><div><span style="line-height:1.5">(Of course, it does mean adding semantics to the declaration syntax in general)</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">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.</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">~Elazar</span></div></div></div>
<br></blockquote></div></div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="CwPjBcH2AQAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="nofollow" target="_blank" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="nofollow" target="_blank" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a><br></blockquote></div></div><div><br><br clear="all"><br>-- <br><div>--Guido van Rossum (<a href="http://python.org/~guido" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2F~guido\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGKI5lSgzXFUM7Y5HtkFAp6sQAxEg';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2F~guido\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGKI5lSgzXFUM7Y5HtkFAp6sQAxEg';return true;">python.org/~guido</a>)</div>
</div></blockquote></div></div></div></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div>--Guido van Rossum (<a href="http://python.org/~guido" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2F~guido\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGKI5lSgzXFUM7Y5HtkFAp6sQAxEg';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2F~guido\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGKI5lSgzXFUM7Y5HtkFAp6sQAxEg';return true;">python.org/~guido</a>)</div>
</div>
</blockquote></div></div>