Storing 'unhashable' types in dictionaries by address
Aahz
aahz at pythoncraft.com
Tue May 27 17:00:15 EDT 2003
In article <l1u1bjgg3d.fsf at budvar.future-i.net>,
Ed Avis <ed at membled.com> wrote:
>
>A dictionary requires that its key type be hashable. This means you
>cannot use lists as keys of a dictionary. Is there any way round
>this?
>
>I would like to store some 'extra' information associated with some
>lists, but do so outside the lists themselves. For example this is
>what I would like to write:
>
>l0 = ['strawberry']
>l1 = ['mint', 'choc']
>l2 = ['vanilla', 'liver']
>
># This dictionary stores some extra comments on each flavour.
>d = {}
>d[l0] = 'yum'
>d[l1] = 'so-so'
>d[l2] = 'ugh'
Other people have tried answering your question, but IMO you're simply
using the wrong approach here. Why not use a class?
class FlavorList:
def __init__(self, flavors=None, comment=None):
if flavors:
self.flavors = flavors
else:
self.flavors = []
self.comment = comment
flavor1 = FlavorList(['strawberry'], 'yum')
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles: boring syntax, unsurprising semantics,
few automatic coercions, etc etc. But that's one of the things I like
about it." --Tim Peters on Python, 16 Sep 93
More information about the Python-list
mailing list