[Python-Dev] Retrieve an arbitrary element from a set without removing it (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Sun Oct 25 06:48:54 CET 2009


Terry Reedy wrote:

In this model, a get() method, or even a getitem with s[k] is k, is only natural. No, if key and value were the same thing, the get method would be nonesense, as Guido said. But since they are not, since the implict key is an abstract class, retrieving the representative, perhaps canonical object given any member is natural. Being able to do so is also a standard practice in mathematics.

To formalise this invariant to some degree: given a set "s" and two items "k1" and "k2", such that "k1 in s" and "k2 in s" and "k1 == k2", there is a single item "k" in s such that "k1 == k" and "k2 == k".

If getitem were to be provided by sets, then the last part of that invariant could be written "s[k1] is s[k2]".

If you actually care about that aspect of the semantics for a particular application, it would be far clearer to use a full dict() and live with the additional memory usage. While I can see how saving that pointer-per-entry might make sense in some circumstances, for most I would see it as a needlessly confusing micro-optimisation.

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list