🔥 BLACK FRIDAY SALE1 course ₹199·any 3 courses ₹399 for 3 months·ALL courses + 1 mock interview ₹599 for 6 monthsClaim offer →

Python · The Python Data Model · Intermediate · 28 min read

Python Operator Overloading and Comparison: Arithmetic, Reflected Operators, total_ordering and Rich Comparisons

Give your types natural operators without surprises. Learn how binary operators dispatch (left operand, reflected right operand, subclass priority and NotImplemented), in-place operators, unary operators, comparison methods and functools.total_ordering, equality versus ordering, sorting custom objects, numeric tower pitfalls with Decimal, and design rules for readable operator use.

Python Operator Overloading and Comparison: Arithmetic, Reflected Operators, total_ordering and Rich Comparisons: lesson illustration

A first look

  • Intermediate level · 28 min read
  • 7 interview questions with model answers
  • The Python Data Model · Python Mastery

You will learn to

  • How a + b is evaluated: add, radd, subclass priority, NotImplemented
  • Arithmetic, in-place and unary operators; sum() and radd
  • Comparison methods, totalordering, and key functions
  • …and 3 more topics inside
Create an account to start

Create a free account to see what this lesson covers

  • Every free lesson in every section, in full
  • Track your progress and pick up where you left off
  • Free practice questions and an incident simulation
  • Takes under a minute: name, email and a password

No card needed. Full access to Python is a paid upgrade you can choose later.