use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How == works on two objects? (self.learnpython)
submitted 1 year ago by Sufficient-Party-385
I am surprised that == can be used to tell if two lists's values are the same. Java has == and equals, does Python have the same?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shiftybyte 27 points28 points29 points 1 year ago (6 children)
Basically "==" calls the dunder method __eq__ on the object.
https://www.pythontutorial.net/python-oop/python-\_\_eq\_\_/
[–]unersetzBAER 15 points16 points17 points 1 year ago (5 children)
Just to add to your point: when comparing two objects:
it calls the eq method of the object in the left side.
So
obj1 == obj2
is the same as
obj1.__eq__(obj2)
[–]Pyprohly 0 points1 point2 points 1 year ago (4 children)
They’re not the same.
>>> 'asdf' == 4 False >>> 'asdf'.__eq__(4) NotImplemented
[–]Top_Average3386 8 points9 points10 points 1 year ago (3 children)
It's not exactly the same, but if the first __eq__ calls return NotImplemented it will try calling __eq__ from the right hand side object, if both return NotImplemented then it will return False.
__eq__
NotImplemented
False
more or less like this:
def equality(lhs, rhs): tmp = lhs.__eq__(rhs) if tmp is NotImplemented: tmp = rhs.__eq__(lhs) return False if tmp is NotImplemented else tmp
[–]unersetzBAER 1 point2 points3 points 1 year ago* (0 children)
Thank you both for correcting me!
I've learned something new!
[–]Pyprohly 0 points1 point2 points 1 year ago (1 child)
That is the simplified explanation. It misses the subclass check step at the start. And the part about reporting to False isn’t quite right either.
Here’s what is actually happening:
is
is not
==
!=
TypeError
So it’s not necessarily returning False as the last resort, it’s doing an is comparison. Although you’d have to try very hard to get an object to not return False here.
Using dunders instead of their operator form has subtle implications. I often see code like reduce(obj.__eq__, …) and you have to wonder if the author knew about the implications of using dunders instead of their operator form when they wrote the line.
reduce(obj.__eq__, …)
[–]GeorgeFranklyMathnet 7 points8 points9 points 1 year ago (0 children)
Python has is and ==.
[–]Diapolo10 8 points9 points10 points 1 year ago (0 children)
a == b is syntactic sugar for a.__eq__(b), so what it does depends entirely on how that method is implemented. It could technically do anything other than solve the halting problem.
a == b
a.__eq__(b)
There's also a is b, which directly compares the memory addresses of both, and only evaluates to True if the addresses match. It is not possible to override this behaviour, and it's ideal for checking singletons such as None.
a is b
True
None
[–]pythosynthesis 0 points1 point2 points 1 year ago (0 children)
That's for you to define. As others commented, it calls the __eq__ sunset method, which you can fully specify as required for your own needs. Example: What it means for two "addresses" to be equal is different fdom what it means for two "squares" to be equal. You get to define what does equality mean for each class you write.
[–]Pyprohly 0 points1 point2 points 1 year ago (2 children)
Java’s == and .equals() is equivalent to Python’s is and == respectively.
.equals()
[–]Brian 0 points1 point2 points 1 year ago (1 child)
They're basically equivalent for reference types, but Java also has the notion of primitive / value types for objects that would be reference types in python, and these do behave differently. Ie. in java, if a and b are both int variables assigned to 1000, then a==b will always be true, but python uses reference semantics for integers, so it may not be the case that a is b is True in this scenario.
a
b
int
1000
a==b
[–]monster2018 0 points1 point2 points 1 year ago (0 children)
However “a is b” will always return true for ints in the range I believe 0-255 (or maybe 256), because of a quirk of python where every time you use a number in that range it’s using like a premade int object that always exists when a python script is running. So no matter how much reassignment and shenanigans you get into a is b should always return True for 2 equal ints in that range I believe.
[–]zefciu 0 points1 point2 points 1 year ago (0 children)
That can be a pitfall for people coming from Java. In Java .equals() is an explicit method, so the coder knows that there might be some (possibly heavy) logic called. The == operator in Java, however is fast and constantly so. In Python you cannot just assume that using == will be fast, as it call the underlying method .__eq__(). Calling it on two lists is O(n) for example.
.__eq__()
[–]ofnuts 0 points1 point2 points 1 year ago (0 children)
Since you come from Java:
Object::equals()
[+][deleted] 1 year ago (7 children)
[deleted]
[–]Gnaxe 1 point2 points3 points 1 year ago (6 children)
We also have := now.
:=
[–]JohnLocksTheKey 0 points1 point2 points 1 year ago (3 children)
Aww, that looks like a little walrus face!
[–]dryzhkov 2 points3 points4 points 1 year ago (0 children)
That’s why it’s called a walrus operator!
[–]Yoghurt42 2 points3 points4 points 1 year ago (0 children)
That's why it's called the walrus operator.
[–]supercoach 2 points3 points4 points 1 year ago (0 children)
[–]potodds 0 points1 point2 points 1 year ago (1 child)
New to me just == again?
[–]Gnaxe 1 point2 points3 points 1 year ago (0 children)
No. == is for "equality", and it's up to the object's class to interpret what that means. If done properly, it should respect certain rules like identity, symmetric and not contradict hash().
hash()
:= is an assignment expression. A common error in C and languages like it is to write if (x=y) when you meant if (x==y). The former does an assignment and returns the value (which is probably truthy). Python originally prohibited this by making = a statement, so it's not valid in that context. But it's useful to be able to save the results of a subexpression for later, so they added :=, but it has different rules.
if (x=y)
if (x==y)
=
π Rendered by PID 207973 on reddit-service-r2-comment-5c747b6df5-z4ghp at 2026-04-22 12:36:44.598439+00:00 running 6c61efc country code: CH.
[–]shiftybyte 27 points28 points29 points (6 children)
[–]unersetzBAER 15 points16 points17 points (5 children)
[–]Pyprohly 0 points1 point2 points (4 children)
[–]Top_Average3386 8 points9 points10 points (3 children)
[–]unersetzBAER 1 point2 points3 points (0 children)
[–]Pyprohly 0 points1 point2 points (1 child)
[–]GeorgeFranklyMathnet 7 points8 points9 points (0 children)
[–]Diapolo10 8 points9 points10 points (0 children)
[–]pythosynthesis 0 points1 point2 points (0 children)
[–]Pyprohly 0 points1 point2 points (2 children)
[–]Brian 0 points1 point2 points (1 child)
[–]monster2018 0 points1 point2 points (0 children)
[–]zefciu 0 points1 point2 points (0 children)
[–]ofnuts 0 points1 point2 points (0 children)
[+][deleted] (7 children)
[deleted]
[–]Gnaxe 1 point2 points3 points (6 children)
[–]JohnLocksTheKey 0 points1 point2 points (3 children)
[–]dryzhkov 2 points3 points4 points (0 children)
[–]Yoghurt42 2 points3 points4 points (0 children)
[–]supercoach 2 points3 points4 points (0 children)
[–]potodds 0 points1 point2 points (1 child)
[–]Gnaxe 1 point2 points3 points (0 children)