0

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: This is a screenshot from my PyCharm IDE

vanbastelaer
  • 368
  • 2
  • 15
  • 2
    I just ran `isinstance([], collections.UserString)` and got `False`. – 0x5453 Feb 02 '21 at 21:44
  • @0x5453 Strange! It returns `True` in my PyCharm IDE running Python 3.8. What are you using? – vanbastelaer Feb 02 '21 at 21:48
  • Can not reproduce `isinstance([], UserString) == True` – wim Feb 02 '21 at 21:54
  • @wim I am not sure what happened. Attached a screenshot just so that no one thinks that I am just being crazy. – vanbastelaer Feb 02 '21 at 22:02
  • Also have `isinstance([], UserString) == False`. Also checked using Pycham's Python Console. – Adam Tokarski Feb 02 '21 at 22:07
  • Hmm, can not reproduce even on 3.8.2 but I am on Linux. What is `repr(UserString)` ? – wim Feb 02 '21 at 22:08
  • @wim >>>repr(UserString) "" – vanbastelaer Feb 02 '21 at 22:09
  • Super weird. Can you show us `UserString.__mro__`? – wim Feb 02 '21 at 22:10
  • @wim Sure: (, , , , , , , ) – vanbastelaer Feb 02 '21 at 22:11
  • All looks normal. Just to confirm, the error message you're seeing is `AttributeError: 'list' object has no attribute 'data'` ? – wim Feb 02 '21 at 22:13
  • @wim That's right, for that trivial example I showed, and after I looked into it, I thought it had to be `__eq__` considering `[]` as an instance of `UserString`. And then apparently my (and only my) Python console confirmed my theory, LOL. If Python is not the issue, my only guess would be Clang...but that's a wild guess? – vanbastelaer Feb 02 '21 at 22:14
  • How Python was installed? Did you compile this Python interpreter yourself? – wim Feb 02 '21 at 22:23
  • You could try to run that with different Python's version, if you have any others. – Adam Tokarski Feb 02 '21 at 22:26
  • @wim I just double checked from my Terminal console instead of PyCharm, it did return `False`. I think the issue is with PyCharm's Python Console somehow "altered" something. – vanbastelaer Feb 02 '21 at 22:26
  • It works on my Pycharm properly. Which version of this IDE you have? – Adam Tokarski Feb 02 '21 at 22:27
  • @AdamTokarski I am using Professional 2020.3.1. – vanbastelaer Feb 02 '21 at 22:27
  • Did you check how it behaves with any other `isinstance` calls? Using another types or objects? – Adam Tokarski Feb 02 '21 at 22:29
  • @AdamTokarski I did try `isinstance([], str)` and got `False`, and that's why I thought `UserString` might be the issue. `isinstance({}, UserString)` returns `True`, while `isinstance((), UserString)` returns `False`. – vanbastelaer Feb 02 '21 at 22:31
  • Clutching at straws here but check if `import builtins; builtins.isinstance is isinstance`. The pydevconsole.py is pretty disgusting, I wouldn't put it past them to monkeypatch that. – wim Feb 02 '21 at 22:36
  • Thanks @wim `builtins.isinstance is isinstance` returns `True` but at this point, only I can figure out what in the world happened to *my* PyCharm. Thanks again. – vanbastelaer Feb 02 '21 at 22:40

0 Answers0