Wie wird das Schlüsselwort 'is' in Python implementiert?
... dasis
Schlüsselwort, das für die Gleichheit in Strings verwendet werden kann.
>>> s = 'str'
>>> s is 'str'
True
>>> s is 'st'
False
Ich habe beide versucht__is__()
und__eq__()
aber sie haben nicht funktioniert.
>>> 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
>>>