I need to use UserString to create my own str class, but its implementation seems problematic.
For example, in the class definition, it reads:
def __eq__(self, string):
if isinstance(string, UserString):
return self.data == string.data
return self.data == string
But since an empty list ([]) is actually an instance of UserString:
isinstance([], UserString) == True
Now this code doesn't work:
s = UserString("")
if s in [None, [], {}, ()]:
# do whatever
because in operator will use UserString's __eq__ to check membership but [] does not have .data attribute. This issue doesn't exist in the built-in str class.
I know this is a trivial, non-realistic example, but anyone encountered this problem before using UserString and what is the best way to circumvent this (maybe method override in my own subclass)? Any other caveats?
Note: I am aware of this SO thread, but I don't think my question is a duplicate of it.
It seems like no one can reproduce isinstance([], UserString) == True. But this is a screenshot from my PyCharm IDE:
