[Python-Dev] Getting values stored inside sets (original) (raw)
Raymond Hettinger python at rcn.com
Sat Apr 4 04:37:38 CEST 2009
- Previous message: [Python-Dev] Getting values stored inside sets
- Next message: [Python-Dev] Documenting the Py3k coercion rules (was Re: Getting values stored inside sets)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Nick Coghlan]
It doesn't quite work the way RDM desribed it - he missed a step.
Thanks for the clarification. We ought to write-out the process somewhere in a FAQ.
It may also be instructive to step through the recipe that answers the OP's original request, http://code.activestate.com/recipes/499299/
The call "get_equivalent(set([1, 2, 3]), 2.0)" wraps the 2.0 in a new object t and calls "t in set([1,2,3])". The set.contains method hashes t using t.hash(self) and checks for an exact match using t.eq(other). Both calls delegate to float objects but the latter also records the "other" that resulted in a successful equality test (i.e. 2 is the member of the set that matched the 2.0). The get_equivalent call then returns the matching value, 2.0.
As far as I can tell, the technique is completely generic and lets you reach inside any function or container to retrieve the "other" value that is equivalent to "self".
Raymond
- Previous message: [Python-Dev] Getting values stored inside sets
- Next message: [Python-Dev] Documenting the Py3k coercion rules (was Re: Getting values stored inside sets)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]