Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Overriding Comparison Operators in Python

When modeling systems in any programming language, we occasionally need our custom objects to be comparable. This is useful for when the class encapsulates a calculation based on some attributes and we want to sort them, or if we want to make direct comparisons to know if two instances are equivalent. In Python, we implement this sort of behavior by defining a suite of double underscore ("dunder") methods on our classes. These allow our custom class instances to be compared using the same operators that we would use if we wanted to know if one integer was greater than or equal to the next. In this hands-on lab, you'll be building a class to model a `Lead` for your sales team so that they can compare and rank leads in the system.

Google Cloud Platform icon
Lab platform
Lab Info
Level
Advanced
Last updated
Sep 23, 2025
Duration
45m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Create the Lead Model and Calculate Lead Score

    Before you can start working on the sorting of leads, you need to have a model. Your coworker has requested that you create the Lead class within the lead.py file next to test_lead.py. Be sure to create the initializer for the class and have it take the proper arguments:

    • name
    • staff_size
    • estimated_revenue
    • effort_factor

    With these implemented, you'll also want to add a method (or calculated property) to return the calculated lead score. Here's the formula to use:

    1 / (staff_size / estimated_revenue * (10 ** (digits_in_revenue - digits_in_staff_size)) * effort_factor)
    

    The automated tests assume that lead_score is a method, but you can adjust the test file if you'd rather it be calculated property instead.

    You can run the unit tests with the following command:

    python -m unittest
    
  2. Challenge

    Implement the Equivalence Comparison Operators

    The first set of operators that you need to implement are the == and != operators. These should compare the lead score of the two Lead instances being compared. You can implement these using the __eq__ and __ne__ methods, and these methods should compare the lead scores of the instances. Implementing these methods should make a few more automated tests pass.

    You can run the unit tests with the following command:

    python -m unittest
    
  3. Challenge

    Implement the Ordering Operators

    With the equivalence operators implemented, you're ready to move onto implementing the ordering operators: <, >, <=, and >=. These operators can be implemented by defining the __lt__, __gt__, __le__, and __ge__ methods, and these methods should compare the lead scores of the instances. Implementing these methods should get the remaining automated tests to pass.

    You can run the unit tests with the following command:

    python -m unittest
    
About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight