Skip to content

Instantly share code, notes, and snippets.

@sloria
Last active December 24, 2022 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloria/5895768 to your computer and use it in GitHub Desktop.
Save sloria/5895768 to your computer and use it in GitHub Desktop.
class Book:
...
def __lt__(self, other):
return (self.author, self.title) < (other.author, other.title)
@axelarge
Copy link

axelarge commented Mar 1, 2014

How about this to reduce duplication (makes sense if there are many variables)

class Book:
    def tupled():
        return (self.author, self.title)

    def __lt__(self, other):
        return self.tupled() < other.tupled()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment