How is the 'is' keyword implemented in Python?
Posted
by Srikanth
on Stack Overflow
See other posts from Stack Overflow
or by Srikanth
Published on 2010-06-07T08:17:54Z
Indexed on
2010/06/07
8:22 UTC
Read the original article
Hit count: 400
... the is
keyword that can be used for equality in strings.
>>> s = 'str'
>>> s is 'str'
True
>>> s is 'st'
False
I tried both __is__()
and __eq__()
but they didn't work.
>>> class MyString:
... def __init__(self):
... self.s = 'string'
... def __is__(self, s):
... return self.s == s
...
>>>
>>>
>>> m = MyString()
>>> m is 'ss'
False
>>> m is 'string' # <--- Expected to work
False
>>>
>>> class MyString:
... def __init__(self):
... self.s = 'string'
... def __eq__(self, s):
... return self.s == s
...
>>>
>>> m = MyString()
>>> m is 'ss'
False
>>> m is 'string' # <--- Expected to work, but again failed
False
>>>
Thanks for your help!
© Stack Overflow or respective owner