免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 11389 | 回复: 26
打印 上一主题 下一主题

请问类的实例可以作为字典的key吗? [复制链接]

Rcfeng 该用户已被删除
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-23 10:42 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
2 [报告]
发表于 2005-09-23 10:49 |只看该作者

请问类的实例可以作为字典的key吗?

The object must be immutable
Rcfeng 该用户已被删除
3 [报告]
发表于 2005-09-23 10:49 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2005-09-23 10:56 |只看该作者

请问类的实例可以作为字典的key吗?

我试了一下没有问题。你的代码是什么,可能是别的错误。

>;>;>; class A:
...     pass
   
>;>;>; a=A()
>;>;>; b={a:1}
>;>;>; b[a]
1
Rcfeng 该用户已被删除
5 [报告]
发表于 2005-09-23 11:05 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2005-09-23 11:21 |只看该作者

请问类的实例可以作为字典的key吗?

原帖由 "Rcfeng"]p2)] 这样是不行的~[/quote 发表:


不使用内置的__cmp__方法就行了

  1. class ip:
  2.    "A Ip class for finding user's area"
  3.    def __init__(self, low_ip, up_ip):
  4.        self.low_ip = low_ip
  5.        self.up_ip = up_ip
  6.    def __str__(self):
  7.        return "Low: %d; Up: %d" % (self.low_ip,self.up_ip)
  8.    def cmpare(self,ip):
  9.        if( ip>;=low_ip and ip<=up_ip ):
  10.            return 0
  11.        elif( ip<low_ip ):
  12.            return -1
  13.        else: return 1
复制代码


可能是有了__cmp__就认为是可变的了
Python文档里找到这么一句
[quote]Instances of a class normally compare as non-equal unless the class defines the __cmp__() method.

__cmp__( self, other)

Called by comparison operations if rich comparison (see above) is not defined. Should return a negative integer if self < other, zero if self == other, a positive integer if self >; other. If no __cmp__(), __eq__() or __ne__() operation is defined, class instances are compared by object identity (``address''). See also the description of __hash__() for some important notes on creating objects which support custom comparison operations and are usable as dictionary keys. (Note: the restriction that exceptions are not propagated by __cmp__() has been removed since Python 1.5.)


看了__hash__的介绍,终于明白了
__hash__( self)

Called for the key object for dictionary operations, and by the built-in function hash(). Should return a 32-bit integer usable as a hash value for dictionary operations. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g., using exclusive or) the hash values for the components of the object that also play a part in comparison of objects. If a class does not define a __cmp__() method it should not define a __hash__() operation either; if it defines __cmp__() or __eq__() but not __hash__(), its instances will not be usable as dictionary keys. If a class defines mutable objects and implements a __cmp__() or __eq__() method, it should not implement __hash__(), since the dictionary implementation requires that a key's hash value is immutable (if the object's hash value changes, it will be in the wrong hash bucket).


If a class does not define a __cmp__() method it should not define a __hash__() operation either; if it defines __cmp__() or __eq__() but not __hash__(), its instances will not be usable as dictionary keys.
Rcfeng 该用户已被删除
7 [报告]
发表于 2005-09-23 11:34 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2005-09-23 11:34 |只看该作者

请问类的实例可以作为字典的key吗?

根据文档里的讲解,用__cmp__也可以,再实现__hash__就行了

我随便试了一下

  1. def __hash__(self):
  2.         return hash(self.low_ip + self.up_ip)
复制代码


是可以的,只是不知道这样hash会不会有问题

论坛徽章:
0
9 [报告]
发表于 2005-09-23 11:38 |只看该作者

请问类的实例可以作为字典的key吗?

你可以这样做

不过要想用dict的话,就得自己实现__hash__方法,保证low_ip和up_ip值都一样的ip的instance其hash值一样,这样就可以做dict的key了

不知道说得对不对,呵呵
Rcfeng 该用户已被删除
10 [报告]
发表于 2005-09-23 11:44 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP