equality

Since when is Python None equal to None?

Since when is Python None equal to None? Question: I always thought that Python nulls are not equal, as is common in many other languages and based on simple logic (if the value is unknown, how can it be equal to another unknown?). However, recently I tried it, and discovered that: Python 3.10.2 >>> None …

Total answers: 3

equality and not equality operators in Python

equality and not equality operators in Python Question: Let’s say I have these two lists: number_list_1 = [1, 2, 3, 4, 5] number_list_2 = [1, 2, 3, 4, 5] then let’s say that I want to know if the two lists are equal (There are a lot of ways to do this in Python and …

Total answers: 1

What happens "behind the scenes" if I call `None == x` in Python?

What happens "behind the scenes" if I call `None == x` in Python? Question: I am learning and playing around with Python and I came up with the following test code (please be aware that I would not write productive code like that, but when learning new languages I like to play around with the …

Total answers: 2

Pydantic exclude field from __eq__ to avoid recursion error

Pydantic exclude field from __eq__ to avoid recursion error Question: I have a pydantic model like this: class SomeModel(pydantic.BaseModel): name: str content: str previous_model: typing.Optional["SomeModel"] = None My code look like this, this is greatly simplified, In my real code there are many and circular dependencies occur by chance occasionally rather than being purposefully created: …

Total answers: 1

Python3 Determine if two dictionaries are equal

Python3 Determine if two dictionaries are equal Question: This seems trivial, but I cannot find a built-in or simple way to determine if two dictionaries are equal. What I want is: a = {‘foo’: 1, ‘bar’: 2} b = {‘foo’: 1, ‘bar’: 2} c = {‘bar’: 2, ‘foo’: 1} d = {‘foo’: 2, ‘bar’: 1} …

Total answers: 2

How do I make an __eq__method to compare a class with 4 init variables?

How do I make an __eq__method to compare a class with 4 init variables? Question: I have the following class: class Building(): def __init__(self, item1, item2, item3, item4): self._item1 = item1 self._item2 = item2 self._item3 = [] self._item3.append(item3) self._item4 = [] self._item4.append(item4) def __eq__(self, building2): Item1 and item2 are both int values, item3 and item4 …

Total answers: 6

How can Python compare strings with integers?

How can Python compare strings with integers? Question: Given the following code: a = ‘1’ if a == 1: print ‘yes’ else: print ‘no’ we get output as no. How is Python comparing a string value to an int here (if a == 1)? In C such a comparison would give an error because this …

Total answers: 1

Are objects with the same id always equal when comparing them with ==?

Are objects with the same id always equal when comparing them with ==? Question: If I have two objects o1 and o2, and we know that id(o1) == id(o2) returns true. Then, does it follow that o1 == o2 Or is this not always the case? The paper I’m working on says this is not …

Total answers: 3

Difference between hash() and id()

Difference between hash() and id() Question: I have two user-defined objects, say a and b. Both these objects have the same hash values. However, the id(a) and id(b) are unequal. Moreover, >>> a is b False >>> a == b True From this observation, can I infer the following? Unequal objects may have the same …

Total answers: 4

Python if not == vs if !=

Python if not == vs if != Question: What is the difference between these two lines of code: if not x == ‘val’: and if x != ‘val’: Is one more efficient than the other? Would it be better to use if x == ‘val’: pass else: Asked By: lafferc || Source Answers: In the …

Total answers: 7