[New-bugs-announce] [issue22562] Singleton pattern for namedtuple

Leo report at bugs.python.org
Mon Oct 6 07:06:50 CEST 2014


New submission from Leo:

Each call of namedtuple will create a new class object even if an equal class, i.e. for the same fields has been created before.
Applying the singleton pattern would be more efficient at least in two respects: 
* it would reduce the number of class objects.
* checking for field names can be done by calling isinstance.

I therefore propose a new boolean keyword argument 'singleton' for the namedtuple function. It should 
default to True. If a pre-existing rather than new class object is returned, the provided class name is 
disregarded. In many cases, the caller will use a local binding anyway.
 
The singleton pattern could be implemented along the following schema:

cache = {}
if not fields in cache:
    cache[fields] = new_namedtuple
return cache[fields]

----------
components: Library (Lib)
messages: 228640
nosy: fhaxbox66 at googlemail.com
priority: normal
severity: normal
status: open
title: Singleton pattern for namedtuple
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22562>
_______________________________________


More information about the New-bugs-announce mailing list