identity
Should I be using is
or ==
?
To check if two objects are equal, use the equality operator (==
).
x = 5
if x == 5:
print("x equals 5")
if x == 3:
print("x equals 3")
# Prints 'x equals 5'
To check if two objects are actually the same thing in memory, use the identity comparison operator (is
).
>>> list_1 = [1, 2, 3]
>>> list_2 = [1, 2, 3]
>>> if list_1 is [1, 2, 3]:
... print("list_1 is list_2")
...
>>> reference_to_list_1 = list_1
>>> if list_1 is reference_to_list_1:
... print("list_1 is reference_to_list_1")
...
list_1 is reference_to_list_1
paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here: https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S
. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.