-1

Is there possibility to compare two 'instances'? I have one variable and one list. Variable have type 'instance', items in list have also the same type. When I compare variable with the same item in list:

cities = [USA, Poland, England, GB, Italy]
variable = Italy

variable == cities[-1]

I received 'False' as output. I'm 100% sure that both elements are the same.

Thanks in advance!

  • 1
    Would need more details here, what is `variable`? What are the context of `list`? You'll likely need to [implement `__eq__` for your class to compare for semantic equality](https://stackoverflow.com/questions/390250/elegant-ways-to-support-equivalence-equality-in-python-classes). – Cory Kramer Apr 13 '21 at 15:23
  • 3
    `list` is a saved namespace, don't use it for variable names. – BoobyTrap Apr 13 '21 at 15:24
  • Try doing 'isinstance(variable, list)' and see if it returns True. – Anmol Batra Apr 13 '21 at 15:40
  • @BobbyTrap is was just example, I know that :) –  Apr 13 '21 at 15:45

1 Answers1

1

There are two types of objects in Python. Mutable, and immutable.

Immutable

  • State cannot be changed.
  • Usually thought of as "primitive" types.
  • int, float, string, tuple, etc.

Mutable

  • State can be updated and changed.
  • list, dict, set, bytearray, any object that is created via the class token.

Depending on the type that you are discussing when you say variable this will affect the operator ==. Immutable types will always be checked against the actual value (e.g. 1 == 1 is True), where mutable types are checked against the object's __eq__ method (which overloads the == sign).

All of the mutable types listed - except new objects initialized with class - have a built-in __eq__ methods that are used when the == sign is present. Assuming you are using your own object, take the following for example:

class Obj:
    def __init__(self, integer):
        self.integer = integer

print(Obj(1) == Obj(1)) # False

Notice that despite integer being equal for each Obj, due to the fact Obj is a mutable type without the __eq__ method Python will check if the objects are equal to each other based on their space in memory- in other words, for it to be True, the object must be the exact same one you initialized.

class Obj:
    def __init__(self, integer):
        self.integer = integer

obj = Obj(1)

print(obj == obj)  # True

To manually overload the == sign, you must use the __eq__ method:

class Obj:
    def __init__(self, integer):
        self.integer = integer

    def __eq__(self, other):
        # Comparison of two integers.
        return self.integer == other.integer

print(Obj(1) == Obj(1))  # True
felipe
  • 7,324
  • 2
  • 28
  • 37
  • How to implement it for string? –  Apr 13 '21 at 15:54
  • Strings already have a built-in `__eq__` method. If you receiving a `False` when doing comparison, it means that they are different. `"hello world" == "helloworld"` will return `False`. – felipe Apr 13 '21 at 16:09
  • I'm asking because my problem concerns strings. Is there any solution? –  Apr 13 '21 at 16:39
  • You would have to format the string in the manner where the ones that are suppose to be equal to each other are indeed equal (e.g. translating things like `à` to `a`, defaulting to lowercase or uppercase, etc.) The fact is, if Python is telling you two strings are unequal, it means that the strings are simply unequal (`"hello" == "Hello"` yields `False`, `"héllo" == "hello"` yields `False`.) – felipe Apr 13 '21 at 16:57
  • Yes yes but my strings are the same, I'm sure of that. Main cause of problem is 'instance' type but as you mentioned before, string already have __eq__ so now, Ikd how to solve it. –  Apr 13 '21 at 17:06
  • To better help you I would advise you editing your original post with what the values of `variable` and `list`. Without those two things there is no way I can help you further - and not because I don't want to, but because it's impossible without the context. – felipe Apr 13 '21 at 17:23
  • You can take anything you want but it must be string. I also edited my post. –  Apr 14 '21 at 07:21
  • `["USA", "Poland", "England", "GB", "Italy"][-1] == "Italy` returns `True` for me. Your example doesn't seem to reflect what is going on. If basic equalities like this didn't work the world as we know would burn! – felipe Apr 14 '21 at 13:27