This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Singleton pattern for namedtuple
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: barry, eric.smith, fhaxbox66@googlemail.com, mark.dickinson, r.david.murray
Priority: normal Keywords:

Created on 2014-10-06 05:06 by fhaxbox66@googlemail.com, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg228640 - (view) Author: Leo (fhaxbox66@googlemail.com) Date: 2014-10-06 05:06
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]
msg228661 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-10-06 11:37
I don't think this is a good idea.  Remember that Python classes are themselves mutable objects, so if a library that you happen to import creates a "Point" namedtuple with fields "x" and "y" and class-level modifications (extra attributes, patched-in methods), any namedtuple you created with fields "x" and "y" would automatically pick up those modifications.  (And conversely, by modifying *your* class, you might adversely affect the behaviour of the library's class.)
msg228678 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-06 13:00
Agreed.  If you want it to be a singleton in your code, use the singleton pattern in your code...but it is hard for me to see why that would be a good idea :) (ie: DRY).  Globally, it does not seem to me that there are likely to be any significant number of identical definitions in non-related codebases, even absent the mutable-class issue.
msg228688 - (view) Author: Leo (fhaxbox66@googlemail.com) Date: 2014-10-06 14:15
A use case for the singleton pattern arises when
- field names are known only at runtime, and
- you have a large number of instances with the same field names.

An example is the storage of metadata for datasets when a hashable
type is needed.

I agree that it will generally be possible to implement the singleton
pattern by wrapping collections.namedtuple. And I recognise that the
singleton pattern would make both old and new code vulnerable to side
effects caused by "mutated" classes. These side effects may well
outweigh any efficiency gains to be reaped.
I therefore withdraw the idea.

Leo

On 06/10/2014, R. David Murray <report@bugs.python.org> wrote:
>
> R. David Murray added the comment:
>
> Agreed.  If you want it to be a singleton in your code, use the singleton
> pattern in your code...but it is hard for me to see why that would be a good
> idea :) (ie: DRY).  Globally, it does not seem to me that there are likely
> to be any significant number of identical definitions in non-related
> codebases, even absent the mutable-class issue.
>
> ----------
> nosy: +r.david.murray
> resolution:  -> rejected
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue22562>
> _______________________________________
>
msg228703 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-10-06 15:19
You might want to check out https://bitbucket.org/ctismer/namelesstuple, which uses a similar approach.
History
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66752
2014-10-06 15:19:37eric.smithsetnosy: + eric.smith
messages: + msg228703
2014-10-06 14:15:36fhaxbox66@googlemail.comsetmessages: + msg228688
2014-10-06 13:00:12r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg228678

resolution: rejected
stage: resolved
2014-10-06 12:01:06barrysetnosy: + barry
2014-10-06 11:37:02mark.dickinsonsetnosy: + mark.dickinson
messages: + msg228661
2014-10-06 09:43:14mark.dickinsonsetversions: + Python 3.5, - Python 3.4
2014-10-06 05:06:50fhaxbox66@googlemail.comcreate