diff options
| author | 2021-05-05 11:19:39 +0200 | |
|---|---|---|
| committer | 2021-05-05 11:19:39 +0200 | |
| commit | b095f48a4ea22a2c7ff33a1668064b78c817b7ad (patch) | |
| tree | c6e63e7912d8e2f0031bed27a1518ddb39f2c0b9 | |
| parent | Merge pull request #1520 from python-discord/str-join-tag (diff) | |
| parent | Merge branch 'main' into is-vs-== (diff) | |
Merge pull request #1517 from python-discord/is-vs-==
Create identity.md
| -rw-r--r-- | bot/resources/tags/identity.md | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bot/resources/tags/identity.md b/bot/resources/tags/identity.md new file mode 100644 index 000000000..fb2010759 --- /dev/null +++ b/bot/resources/tags/identity.md @@ -0,0 +1,24 @@ +**Identity vs. Equality** + +Should I be using `is` or `==`? + +To check if two objects are equal, use the equality operator (`==`). +```py +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`). +```py +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") +# Prints 'list_1 is reference_to_list_1' +``` |