[英]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.