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: fhaxbo...@googlemail.com
priority: normal
severity: normal
status: open
title: Singleton pattern for namedtuple
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22562>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to