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.

Author Chris.Colbert
Recipients Chris.Colbert, ncoghlan, rhettinger
Date 2011-03-21.22:13:29
SpamBayes Score 1.9349689e-11
Marked as misclassified No
Message-id <1300745611.16.0.624548709174.issue11628@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2011-03-21 22:13:31Chris.Colbertsetrecipients: + Chris.Colbert, rhettinger, ncoghlan
2011-03-21 22:13:31Chris.Colbertsetmessageid: <1300745611.16.0.624548709174.issue11628@psf.upfronthosting.co.za>
2011-03-21 22:13:29Chris.Colbertlinkissue11628 messages
2011-03-21 22:13:29Chris.Colbertcreate