[New-bugs-announce] [issue11628] cmp_to_key generated class should use __slots__

Chris Colbert report at bugs.python.org
Mon Mar 21 23:13:31 CET 2011


New submission from Chris Colbert <sccolbert at gmail.com>:

The cmp_to_key func acts as a class factory for generating key objects that behave according to a user defined cmp function. 

Many patterns/libs that make use of key functions (for example blist and the SortedCollection recipe) store the the results of the key function internally to provide faster subsequent operations. For large datasets, the number of key objects can become large, and it makes sense to keep their memory footprint as small as possible. 

>From this stance, I propose to add "__slots__ = ('obj',)" to the class definition of "K" in cmp_to_key to prevent the creation of the un-needed instance dict and save the memory. 

I find it hard to conjure up a case where a user would need to weakref or add attributes to the return value of a key func. But should that need arise, that functionality could be provided as optional kwargs to cmp_to_key, or just have the user subclass the return value of cmp_to_key. Alternatively, the addition of __slots__ could be specified as a kwarg to cmp_to_key. 

I'd be happy to make a patch for any of these (or alternative) options.

----------
components: Library (Lib)
messages: 131691
nosy: Chris.Colbert, ncoghlan, rhettinger
priority: normal
severity: normal
status: open
title: cmp_to_key generated class should use __slots__
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4

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


More information about the New-bugs-announce mailing list