Popular recipes tagged "dictionary" and "class" but not "attributes"http://code.activestate.com/recipes/tags/dictionary+class-attributes/2012-07-24T21:24:14-07:00ActiveState Code RecipesDoubleDict (Python)
2012-07-24T21:24:14-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578224-doubledict/
<p style="color: grey">
Python
recipe 578224
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/dictionary/">dictionary</a>).
</p>
<p>After seeing requests for being able to access keys in a dictionary by value, the following recipe was born. It creates the <code>DoubleDict</code> class and allows just that. To ensure that only one key is returned when accessing it by value, values must be unique just as keys are unique, and this rule is automatically enforced. Most dictionary methods are supported, and many more are added to allow working with the dictionary from the view of the values instead of the keys. Several optional metaclasses are also provided to enable optional features in the <code>DoubleDict</code> class such as data consistency checks and atomic method execution.</p>