python bijection

Gabriel Genellina gagsl-py2 at
Sat Nov 28 03:36:13 CET 2009

En Fri, 27 Nov 2009 15:12:36 -0300, Francis Carr <coldtortuga at>  

> 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 as <a
> href="">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

More information about the Python-list mailing list