<div dir="ltr"><div class="gmail_default" style="font-family:monospace,monospace"><span style="font-family:arial,sans-serif">On Fri, Jul 21, 2017 at 8:49 AM, Serhiy Storchaka </span><span dir="ltr" style="font-family:arial,sans-serif"><<a href="mailto:storchaka@gmail.com" target="_blank">storchaka@gmail.com</a>></span><span style="font-family:arial,sans-serif"> wrote:</span><br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">20.07.17 04:35, Alexander Belopolsky пише:<span class="gmail-"><br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
On Wed, Jul 19, 2017 at 9:08 PM, Guido van Rossum <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
The proposal in your email seems incomplete<br>
</blockquote>
<br>
The proposal does not say anything about type((x=1, y=2)).  I assume<br>
it will be the same as the type currently returned by namedtuple(?, 'x<br>
y'), but will these types be cached? Will type((x=1, y=2)) is<br>
type((x=3, y=4)) be True?.<br>
</blockquote>
<br></span>
Yes, this is the key problem with this idea.<br>
<br>
If the type of every namedtuple literal is unique, this is a waste of memory and CPU time. Creating a new type is much more slower than instantiating it, even without compiling. If support the global cache of types, we have problems with mutability and life time. If types are mutable (namedtuple classes are), setting the __doc__ or __name__ attributes of type((x=1, y=2)) will affect type((x=3, y=4)). How to create two different named tuple types with different names and docstrings? </blockquote><div><br></div><div class="gmail_default" style="font-family:monospace,monospace">How about just making a named namedtuple if you want to mutate the type? Or perhaps make help() work better for __doc__ attributes on instances. Currently,</div><div class="gmail_default" style="font-family:monospace,monospace"><br></div><div class="gmail_default" style="font-family:monospace,monospace"><div class="gmail_default">>>> class Foo: pass</div><div class="gmail_default">... </div><div class="gmail_default">>>> f = Foo()</div><div class="gmail_default">>>> f.__doc__ = "Hello"</div><div class="gmail_default">>>> help(f)</div><div><br></div><div>does not show "Hello" at all.</div><div><br></div></div><div class="gmail_default" style="font-family:monospace,monospace"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">In Python 2 all types are immortal, in python 3 they can be collected as ordinary objects, and you can create types dynamically without a fear of spent too much memory. If types are cached, we should take care about collecting unused types, this will significantly complicate the implementation.<div class="gmail-HOEnZb"><div class="gmail-h5"><br></div></div></blockquote><div><br></div><div class="gmail_default" style="font-family:monospace,monospace">Hmm. Good point. Even if making large amounts of arbitrary disposable anonymous namedtuples is probably not a great idea, someone might do it. Maybe having a separate type for each anonymous named tuple is not worth it. After all, keeping references to the attribute names in the object shouldn't take up that much memory. And the tuples are probably often short-lived.</div><div class="gmail_default" style="font-family:monospace,monospace"><br></div><div class="gmail_default" style="font-family:monospace,monospace">Given all this, the syntax for creating anonymous namedtuples efficiently probably does not really need to be super convenient on the Python side, but having it available and unified with that structseq thing would seem useful.</div><div class="gmail_default" style="font-family:monospace,monospace"><br></div><div class="gmail_default" style="font-family:monospace,monospace">-- Koos</div></div><br clear="all"><div><br></div>-- <br><div class="gmail_signature">+ Koos Zevenhoven + <a href="http://twitter.com/k7hoven" target="_blank">http://twitter.com/k7hoven</a> +</div>
</div></div>