URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 4 » q:__cmp__返回值的困惑
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
q:__cmp__返回值的困惑
q:__cmp__返回值的困惑 - parnla [2006-03-01 14:53 | 1,371 byte(s)]
 
Re: q:__cmp__返回值的困惑 - limodou [2006-03-02 09:39 | 483 byte(s)]
 
Re: q:__cmp__返回值的困惑 - parnla [2006-03-02 11:11 | 876 byte(s)]
 
Re: q:__cmp__返回值的困惑 - passworld [2006-03-02 11:42 | 877 byte(s)]
 
Subject: q:__cmp__返回值的困惑
Author: parnla    Posted: 2006-03-01 14:53    Length: 1,371 byte(s)
[Original] [Print] [Top]

class dog:
def __cmp__(self,other):
if other >0:
print "1"
return 1
elif other < 0:
print other
print "2"
return -1
else:
print other
return 0

下面是我执行的结果;
>>> x = dog()
>>> print (x >2)
1
True
>>> print (x>2)
1
True
>>> print (x > -2)
-2
2
False
>>> if (-1): print "test"

test
>>> print (x ==0)
0
True

当执行x > 2

if other >0:
print "1"
return 1

返回值是1,所以print (x >2)是True,但是x > -2时,


elif other < 0:
print other
print "2"
return -1

返回值是-1,print (x >2)应该True,
当执行的结果是False. 为什么,是我一开始就理解错了?

[Original] [Print] [Top]
Subject: Re: q:__cmp__返回值的困惑
Author: limodou    Posted: 2006-03-02 09:39    Length: 483 byte(s)
[Original] [Print] [Top]
没看懂你要说什么.程序是对的呀.是不是你不理解为什么

x > 2 是True
而 x > -1 是 False呢?

主要的原因就是你的 __cmp__ 并没有比较x与other的关系,而只是比较了other与0的关系.因此不管你是 x > 2 还是 x < 2 应该都是一个值,那么你的__cmp__的处理其实只是用来判断other的值,与x无关.所以当 x > -2 时.因为 -2 < 0 所以返回了False.而 x > 2 ,因为 2> 0 所以返回了True.这里面因为没有x的事,所以造成了混乱.
----
[Original] [Print] [Top]
Subject: Re: q:__cmp__返回值的困惑
Author: parnla    Posted: 2006-03-02 11:11    Length: 876 byte(s)
[Original] [Print] [Top]
x >2 时,是比较2>0然后返回1。所以结果是true.
而x>-1时,是返回-1结果是false
是这样么?-1表示假么?不是0,[],(),{},NON才表示假么?
我把return 1换为False,return -1换为True后,
下面是执行的结果,这样能理解,
但是为什么x>0,x==0的结果不一样,他们都走的最后的分支,都return 0,
但一个是True,一个是False,被搞糊涂了
>>> x>2
1
False
>>> x<2
1
False
>>> x>-1
-1
2
True
>>> x>-1
-1
2
True
>>> x > 0
0
False
>>> x == 0
0
True



[Original] [Print] [Top]
Subject: Re: q:__cmp__返回值的困惑
Author: passworld    Posted: 2006-03-02 11:42    Length: 877 byte(s)
[Original] [Print] [Top]
你要定义 __cmp__ ,为什么不先把 __cmp__ 的定义看清楚?__cmp__ 应该返回什么值文件写的清清楚楚。

3.3.1 Basic customization:

__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.)

----
[Original] [Print] [Top]
« Previous thread
安装MySQLdb的问题,找不到shared obect文件
Python 编 程
4
Next thread »
[精华] 邮件直投的改进版
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:10:56, cost 0.065444946289062 ms.