[Python-ideas] namedtuple literals [Was: RE a new namedtuple]

Serhiy Storchaka storchaka at gmail.com
Fri Jul 21 01:49:20 EDT 2017


20.07.17 04:35, Alexander Belopolsky пише:
> On Wed, Jul 19, 2017 at 9:08 PM, Guido van Rossum <guido at python.org> wrote:
>> The proposal in your email seems incomplete
> 
> The proposal does not say anything about type((x=1, y=2)).  I assume
> it will be the same as the type currently returned by namedtuple(?, 'x
> y'), but will these types be cached? Will type((x=1, y=2)) is
> type((x=3, y=4)) be True?.

Yes, this is the key problem with this idea.

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? 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.



More information about the Python-ideas mailing list