Why == doesn't returns true in this case?
Question
I'm jython/python bukkit plugin developer and I'm stuck here. I was always using == in cases like this one, and everything worked perfect. What's the problem? Here's the code:
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)
When I run code, I get code process to the "clickable1 ok" logger, so it stops checking if iteminslot == currentKey... But when I print out Iteminslot and Currentkey, they are same!
20:41:00 [INFO] Iteminslot: DIAMOND_SWORD
20:41:00 [INFO] CurrentKey: DIAMOND_SWORD
20:41:01 [INFO] clickable1 ok
Where am I making mistakes? Thanks for reading/answering! :)
Solution
You're trying to compare a byte-string with a unicode string, they are not always equal. You should decode/encode them properly before comparing:
>>> 'ć' == u'ć'
False
>>> 'ć' == u'ć'.encode('utf-8')
True
>>> 'ć'.decode('utf-8') == u'ć'
True
Secondly as @BrenBarn mentioned, two objects can print out to a same string. But that doesn't means they're equal:
>>> class foo:
def __str__(self):
return 'foo'
...
>>> class bar:
def __str__(self):
return 'foo'
...
>>> print (foo())
foo
>>> print (bar())
foo
>>> foo == bar
False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow