[Python-ideas] Fwd: Anonymous namedtuples

Guido van Rossum guido at python.org
Tue Apr 19 13:35:45 EDT 2016

On Tue, Apr 19, 2016 at 9:58 AM, João Bernardo <jbvsmo at gmail.com> wrote:

> On Tue, Apr 19, 2016 at 12:06 PM, Guido van Rossum <guido at python.org>
> wrote:
>> Every call to namedtuple() creates a new class, which is a very expensive
>> operation. On my machine the simplest namedtuple call taks around 350 usec.
> Isn't that because the code for namedtuple is using exec instead a more
> pythonic approach of metaclassing to make it "faster" but taking longer to
> actually create the class?
> http://bugs.python.org/issue3974

That's part of it, but no matter how you do it, it's still creating a new
class object, you can't get around that. And class objects are just
expensive. Any idiom that ends up creating a new class object for each
instance that's created is a big anti-pattern.

> Maybe instead of changing namedtuple (which seems to be a taboo), there
> could be a new anonymous type. Something built-in, with its own syntax.

That's essentially what the (x=12, y=16) proposal is about, IIUC -- it
would just be a single new type, so (x=12, y=16).__class__ would be the
same class object as (a='', b=3.14).

But I have serious reservations about that idiom too.

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160419/56d1bf48/attachment.html>

More information about the Python-ideas mailing list