En Fri, 27 Nov 2009 15:12:36 -0300, Francis Carr <coldtort...@gmail.com> escribió:

I was really inspired by this discussion thread! :-)

After much tinkering, I think I have a simpler solution.  Just make
the inverse mapping accessible via an attribute, -AND- bind the
inverse of -THAT- mapping back to the original.  The result is a
python dict with NO NEW METHODS except this inverse-mapping
attribute.  I have posted it on code.activestate.com as <a
href="http://code.activestate.com/recipes/576968/";>Recipe 576968:
Flipdict -- python dict that also maintains a one-to-one inverse
mapping</a>

Nice idea! Just a couple of comments:

Instead of:
        self._flip = dict.__new__(self.__class__)
I'd write:
        self._flip = self.__class__()
unless I'm missing something (but see the next point).

Also, although Python's GC is able to handle them, I prefer to avoid circular references like those between x and x._flip. Making self._flip a weak reference (and dereferencing it in the property) should be enough.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to