首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python 运算符重载的有关问题

2013-06-25 
python 运算符重载的问题classDog:...def__cmp__(self,other):... ifother0:return1... elifother0:

python 运算符重载的问题

>>>  class  Dog:
...         def  __cmp__(self,other):
... if  other  >  0  :  return  1;
... elif  other  <  0  :  return  -1;
... else:  return  0
...
>>>  dog  =  Dog()
>>>  if  dog  >  -10  :  print  "ok"
...
>>>  if  dog  <  -10  :  print  "ok"
...
ok
>>>  if  dog  ==  0  :  print  "ok"
...
ok
>>>


这段代码一直不理解,为什么结果会是这样的,有懂的帮忙详细解释一下,谢谢了。 python??__cmp__
[解决办法]
if??dog??>??-10??其实就是
if??dog.__cmp__?(-10)  大于?0?

其余的两个我想应该你自己猜出来才行

热点排行