0

I have a list of objects and I want to compare a string(actually int but I can cast it to str) to check of any of the object in the the list has that string as a member.

Now there is way in which I can iterate over all objects and compare val1 member with my string, but I was wondering if it is possible to do the way below?

class Check:

    def __init__(self, val1):
        self.val1 = val1

    def __str__(self):
        return str(self.val1)


o1 = [Check(100)]
test = "100"

for x in o1:
    print(x)
print(test in o1)

but the print(test in o1) return false

ooo
  • 512
  • 1
  • 7
  • 27

4 Answers4

3

You may overide the __eq__ operator, in case the other parameter is a string, be nice also to keep a comparison to another Check object

def __eq__(self, other):
    if isinstance(other, str):
        return other == str(self.val1)
    if isinstance(other, Check):
        return other.val1 == self.val1
azro
  • 53,056
  • 7
  • 34
  • 70
2

Just because your method returns a string representation which is equal to the string representation of some other object does not make those objects compare equal.

If you wanted instances of your class to be treated as equal to other objects that have the same string representation, then you could add an __eq__ method that implements this:

    def __eq__(self, other):
        return str(self) == str(other)

You will then find that in your test case that test in o1 evaluates True.

Note that if you do this then Check(100) == Check('100') will also evaluate True (even though their val1 properties are of different types). You do not specifically say whether you want this, but the fact that you want Check(100) == '100' to evaluate True strongly suggests that this is this case.

alani
  • 12,573
  • 2
  • 13
  • 23
0

Full working example with typing.

from typing import List


class Check:
    def __init__(self, val1: str or int):
        self.val1 = val1

    def __str__(self):
        return str(self.val1)

    def __eq__(self, other):
        comparison_type = str

        if not isinstance(self.val1, comparison_type):
            self.val1 = comparison_type(self.val1)

        if not isinstance(other, comparison_type):
            other = comparison_type(other)

        return self.val1 == other


if __name__ == "__main__":
    o1: List[Check] = [Check(100)]
    test: str = "100"
    print(test in o1)
mlisthenewcool
  • 539
  • 3
  • 14
0

O1 consists of a list of instances, since you instantiate the Check class, and you're comparing it to a string, which of course won't work. Test is a string object, while the one object o1 contains is, on the other hand, a Check object (that's what type() would return if you ran it on it).

Note that __str__ has nothing to do with turning the object into a string: it's simply used for 'pretty printing' (eg as done by the print function) and is called by print() when ran on the given object, which is contrast to __repr__, which is, conversely, meant for more of a raw, technical, implementation-related display.

Something else needs to be overloaded for the object to support comparisons and in order to define exactly how it should behave in such situations: ie __eq__ and __ne__.

Lamanus
  • 12,898
  • 4
  • 21
  • 47
Dgjccgr3_
  • 1
  • 2