我是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
我在哪里犯错误?感谢您阅读/回答! :)
答案 0 :(得分:1)
您正在尝试将字节字符串与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