OLD cs112 notes
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Equality Figuring out whether two things are the same or not might seem like it should be the easiest job in the world. Unfortunately that's not the case. Equality, identity, and comparisons are surprisingly complicated topics in any programming language. Python is no exception. Our goal in this class is to introduce some of the subtleties that come up when talking about these topics. The approaches to handling these subtleties, and the syntax we'll use, are specific to Python. While they might present in different ways, these issues appear in any programming language, so let's try not to get too bogged down in Python specifics! ## Equality vs identity (on literals) Python integers, strings, and boolean values are *literals*: they're immutable values stored directly in memory. If we assign ```python a = 365 b = 365 ``` Then, in our memory model, we've put the value `365` at `loc 0`, and the value `365` at `loc 1`. Our program dictionary directs `a` to `loc 0` and `b` to `loc 1`. `a` and `b` are equal, according to the `==` operator: ```python >>> a == b True ``` This operator, on integers, compares their literal values. But there's at least one difference between `a` and `b`: the locations that they point to in memory. Python's `id` function lets us inspect this. ```python >>> id(a) 140670548369360 >>> id(b) 140670547931312 ``` There's a fundamental operator in Python, `is`, that compares two expressions by the location that each occupies in memory. `a is b` will return `True` only when `a` and `b` have the same `id`. ```python >>> a == b True >>> a is b False >>> a is a True ``` We often call `==` the *equality* operator, and `is` the *identity* operator. Equality is a function that takes two arguments and returns a boolean. Its implementation depends on what class the inputs belong to. Identity also takes two arguments and returns a boolean, but its implementation is always the same: check that the two locations in memory match. We sometimes use the terminology *relational equality* for `==` and *reference equality* for `is`. Checking identity is always very fast. Consider two strings, each 100,000 characters long, that differ only on the last character. Checking relational equality would involve comparing each corresponding pair of characters, and wouldn't return `False` until after checking all 100,000 pairs. Checking identity, on the other hand, involves just one comparison of two integers. On the other hand, identity is a very precise thing to check. Most of the time we're really interested in equality. ## Subtleties with identity on literals Let's modify our example above just a bit: ```python >>> a = 255 >>> b = 255 >>> a == b True >>> a is b True ``` What? Why is the behavior for 255 different from the behavior for 365? Unsurprisingly, small integers are used more often in practice than large ones. Python knows this, and so it can optimize certain kinds of computations by automatically loading the integers -5 to 256 into locations in memory. Assigning `a = 255` points `a` to this pre-loaded location, and similarly for `b`, so `a` and `b` are referentially equal. This process is called *interning*. Python does something similar for strings; the details of which string literals get interned vary, depending on which version of Python you're using and how you run your code. This means you shouldn't write code whose correctness depends on certain things being identical! Interning is a useful optimization, but not something you should assume. There's another thing that gets interned: `None`. Any two `None`s should always be identical. This is something you *can* assume, that has real effects, as we'll see in a minute! ## Equality on classes Let's define a new class, with nothing special in it yet. ```python class Book: def __init__(self, author: str, title: str, edition: int): self.author = author self.title = title self.edition = edition ``` It's natural to want to compare books. But how does relational equality work on our custom class? ```python >>> book1 = Book("Emily Bronte", "Wuthering Heights", 1) >>> book2 = Book("Emily Bronte", "Wuthering Heights", 1) >>> book1 is book2 False >>> book1 == book2 False >>> book1 == book1 True ``` We've created two different "copies" of Wuthering Heights and stored them at different locations in memory. It's no surprise that they aren't identical. But we never told Python how to compare books; it doesn't know how to check equality, so it falls back to checking identity. We need to add a class method to `Book` that computes this equality. ```python class Book: def __init__(self, author: str, title: str, edition: int): self.author = author self.title = title self.edition = edition def __eq__(self, other): return self.author == other.author \ and self.title == other.title \ and self.edition == other.edition ``` Evaluating `book1 == book2` is exactly the same as evaluating `book1.__eq__(book2)`. Note: among other things, dataclasses will take care of the `__eq__` method for us, defining it in the obvious way. ```python @dataclass class Movie: title: str director: str >>> Movie("Parasite", "Bong Joon-ho") == Movie("Parasite", "Bong Joon-ho") True ``` ## Redefining equality There's another catch. What if I tried to do a comparison `book1 == None`? `None` doesn't have an `author` field, so this will throw an exception. We could check in our `__eq__` method that the objects we're comparing have the same type: ```python def __eq__(self, other): if isinstance(other, Book): return self.author == other.author \ and self.title == other.title \ and self.edition == other.edition else: return False ``` But since `None` is always interned, it's often safer and more predictable to test identity: check for `book1 is None` instead of `book1 == None`. `is` will never crash, and will always behave correctly here. You might find it surprising to change `_eq__` in this way: we're refining our notion of what equality is. But this is the power of relational equality: Python doesn't impose any rules about how we implement it. In some applications, we might want to consider two books to be equal regardless of whether their edition numbers match. ```python def __eq__(self, other): if isinstance(other, Book): return self.author == other.author \ and self.title == other.title else: return False >>> Book("Emily Bronte", "Wuthering Heights", 1) == Book("Emily Bronte", "Wuthering Heights", 2) True ``` Or maybe we want to go crazy: ```python import random class IntWrapper: def __init__(self): self.val = random.randint(0, 1000) def __lt__(self, other): return self.val < other.val def __gt__(self, other): return self.val > other.val def __eq__(self, other): return random.randint(0, 1) == 0 def __repr__(self): return repr(self.val) iw = IntWrapper() >>> iw == iw False >>> iw == iw True >>> iw == iw True >>> iw == iw False >>> iw == iw True ``` But think about how much this would break. Lots of things depend on a "sensible" implementation of `eq`. Remember `quick_sort`: ```python def quick_sort(l: list) -> list: if len(l) <= 1: return l[:] pivot = l[0] # many other choices possible smaller = [x for x in l if x < pivot] larger = [x for x in l if x > pivot] same = [x for x in l if x == pivot] smaller_sorted = quick_sort(smaller) larger_sorted = quick_sort(larger) return smaller_sorted + same + larger_sorted >>> rand_list = [IntWrapper() for i in range(10)] >>> rand_list [236, 22, 190, 289, 124, 90, 197, 462, 607, 113] >>> quick_sort(rand_list) [22, 190, 124, 197, 113, 90, 113, 90, 113, 190, 124, 197, 190, 289, 124, 90, 462, 607, 113, 289, 607, 607] ``` So what does "sensible" mean? Think about it! <details> <summary><B>Think, then Click!</B></summary> * Equality should always be *reflexive*: everything is equal to itself. You may even wonder what "itself" could mean here, but we have a notion for that: identity. So to refine this thought: *if `a is b` returns `True`, then `a == b` should return `True`*. * Equality should be *symmetric*: if `a == b`, then `b == a`. * Equality sould be *transitive*: if `a == b` and `b == c`, then `a == c`. You may have heard the term *equivalence relation* to describe a relation that is reflexive, symmetric, and transitive. Indeed, `__eq__` can be read as `equivalent` instead of `equal`! Furthermore: * Equality should be *deterministic*. Our use of `random` is probably not a good idea. * If we define other operators like `__lt__` as we did above, equality should interact in a reasonable way with these. </details> ## Morals Most programming languages have this same distinction between identity and equality. Depending on the language, it's rare that you *need* to think about identity. But understanding the concept can help you debug confusing behavior, and with careful use, you can use it to write very efficient code. Similarly, most languages give us a lot of freedom when it comes to defining equality. (In languages that don't, we often see other complications.) But with this freedom comes the responsibility not to abuse it! Bad equivalences can be a huge source of bugs.

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully