Hi!
> Just the hash is not enough. You need to define equality, too:
Thanks a million for clearing that up.
Cheers!
/Joel
--
http://mail.python.org/mailman/listinfo/python-list
Joel Hedlund wrote:
> There's one thing about dictionaries and __hash__() methods that puzzle
> me. I have a class with several data members, one of which is 'name' (a
> str). I would like to store several of these objects in a dict for quick
> access ({name:object} style). Now, I was thinking tha
Beautiful!
But how come my attempt didn't work? I've seen docs that explain how __hash__()
methods are used to put objects in dict buckets:
http://docs.python.org/ref/customization.html#l2h-195
But if it's really hash(str(o)) that's used for dict keys, what good are
__hash__() methods? Or am I
Actually, come to think of it, __str__ works just as well.
>>> class NamedThing(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
>>> d = {}
>>> d[a] = 1
>>> d[b] = 50
>>> d
{<__main__.NamedThing object at 0x00C528D0>: 1, <__main__.
Joel Hedlund a écrit :
(snip)
> How illegal is it? If I document it and put it in an opensource project,
> will people throw tomatoes?
Don't know, but they'll sure do if you insist on top-posting !-)
--
http://mail.python.org/mailman/listinfo/python-list
Hi!
Thanks for the quick response!
> Although this is a bit illegal, because repr is not supposed to be used
> this way.
How illegal is it? If I document it and put it in an opensource project, will
people throw tomatoes?
/Joel
[EMAIL PROTECTED] wrote:
> Use __repr__. Behold:
>
>
cla
Use __repr__. Behold:
>>> class NamedThing(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
>>> a = NamedThing("Delaware")
>>> b = NamedThing("Hawaii")
>>> d = {}
>>> d[a] = 1
>>> d[b] = 50
>>> print d
{Delaware: 1, Hawaii: 50}
Hi!
There's one thing about dictionaries and __hash__() methods that puzzle me. I
have a class with several data members, one of which is 'name' (a str). I would
like to store several of these objects in a dict for quick access
({name:object} style). Now, I was thinking that given a list of obj