繁体   English   中英

为什么==在这种情况下不返回true?

[英]Why == doesn't returns true in this case?

我是jython / python bukkit插件开发人员,我被困在这里。 在这种情况下,我总是使用==,并且一切正常。 有什么问题? 这是代码:

        lastslot = event.getNewSlot()
        iteminslot = event.getPlayer().getInventory().getItem(lastslot).getType()
        print "Iteminslot: %s "%iteminslot
        print "CurrentKey: %s"%currentKey
        if clickable1 == "false":
           log.info("clickable1 ok")
           if iteminslot == currentKey:
              log.info("iteminslot ok")
              event.getPlayer().addPotionEffect(potion_effect) 

当我运行代码时,我将代码处理到“ clickable1 ok”记录器中,因此它停止检查iteminslot == currentKey ...但是当我打印出Iteminslot和Currentkey时,它们是相同的!

20:41:00 [INFO] Iteminslot: DIAMOND_SWORD
20:41:00 [INFO] CurrentKey: DIAMOND_SWORD
20:41:01 [INFO] clickable1 ok

我在哪里犯错? 感谢您的阅读/回答! :)

您正在尝试将字节字符串与unicode字符串进行比较,但它们并不总是相等的。 在比较之前,您应该正确解码/编码它们:

>>> 'ć' == u'ć'
False
>>> 'ć' == u'ć'.encode('utf-8')
True
>>> 'ć'.decode('utf-8') == u'ć'
True

其次,如@BrenBarn所述,两个对象可以打印到同一字符串。 但这并不意味着它们是平等的:

>>> class foo:
    def __str__(self):
        return 'foo'
...     
>>> class bar:
    def __str__(self):
        return 'foo'
...     
>>> print (foo())
foo
>>> print (bar())
foo
>>> foo == bar
False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM