Featured Article

From Past Builtins Import Cmp

William Aug 14, 2026

The line of code from past.builtins import cmp often catches the eye of Python developers, especially those who have been using it for a while. It's among the first lines in the classic "Hello, World!" example, but it's more than just boilerplate. It's a window into the language's history and a learning opportunity about deprecated features.

from past builtins import cmp
from past builtins import cmp

Python's cmp function was a way to perform simple comparisons between two objects, returning -1, 0, or 1 depending on whether the first object was less than, equal to, or greater than the second. However, the developers eventually decided to remove it in Python 3, marking the beginning of a new era in Python's evolution.

McGee & Co. | Jonas Sconce Antique Brass | Linen
McGee & Co. | Jonas Sconce Antique Brass | Linen

The Evolution of Python Comparisons

The simple 'less than', 'greater than', and 'equal to' comparisons (<, >, ==) in Python have been around for a long time. They're fundamental to the language, used in conditional statements, sorting algorithms, and many other contexts.

fireplace with built in bookshelves
fireplace with built in bookshelves

However, in the early days of Python, these operators weren't quite enough. Enter the cmp function, designed to simplify complex comparisons.

What was cmp used for?

from past builtins import cmp
from past builtins import cmp

The cmp function was a way to perform a custom comparison operation. It was often used in sorting algorithms or other contexts where a custom comparison was necessary. It simplified the task of writing complex comparison logic.

For instance, say you wanted to sort a list of strings, but not alphabetically. With cmp, you could define a custom comparison function that sorted based on some other criterion. This is where cmp's power lay: it allowed for complex, application-specific sorting and comparison logic.

Why was cmp deprecated?

Home Renovation
Home Renovation

While cmp served its purpose well, Python's developers started to move towards a more expression-based way of programming. They wanted to get rid of the old, functional model where you directly define what you want the function to do (like cmp(-1) if x < y else 1 if x > y else 0).

The new model, encapsulated in the 'Rich Comparisons' added in Python 3, involves objects directly saying how they compare to each other (x < y is now defined by `__lt__` method). This is more natural, more powerful, and more extensible - you can now add entirely new comparison operators to your objects.

Understanding the Deprecation

Living Room Built Ins, Great Rooms
Living Room Built Ins, Great Rooms

The deprecation of cmp means that it's no longer considered good practice to use it in new Python code. If you find yourself writing cmp(x, y), you should step back and think about what you're really trying to do.

If you're writing a sorting algorithm, use key functions. If you're performing complex comparisons, define your own comparison methods (__lt__, __eq__, etc.). This will not only help you write more modern, Pythonic code, but it will also help you gain a deeper understanding of how Python works.

from past builtins import cmp
from past builtins import cmp
from past builtins import cmp
from past builtins import cmp
Custom Built-Ins Around the Fireplace
Custom Built-Ins Around the Fireplace
First Home, House Plans, Fireplace, Cottage, Home Decor, Home Décor
First Home, House Plans, Fireplace, Cottage, Home Decor, Home Décor
Living Room
Living Room
Reno
Reno
from past builtins import cmp
from past builtins import cmp
from past builtins import cmp
from past builtins import cmp
Site Visit
Site Visit
built in shelves around fireplace drawers - Google
built in shelves around fireplace drawers - Google
How to Build DIY Bookshelves For Built-Ins (Step-by-Step Pictures)
How to Build DIY Bookshelves For Built-Ins (Step-by-Step Pictures)
Custom Built-In Wall Shelving That Blends Storage & Style
Custom Built-In Wall Shelving That Blends Storage & Style
Closet
Closet
from past builtins import cmp
from past builtins import cmp
Clancy Construction Custom Cabinetry - Falmouth, M
Clancy Construction Custom Cabinetry - Falmouth, M
a man working on a ladder in a room with white walls and wood framing around it
a man working on a ladder in a room with white walls and wood framing around it
Cozy Basement Built In Ideas That Make Every Inch Work Harder
Cozy Basement Built In Ideas That Make Every Inch Work Harder
With a number of projects, we are always being pulled in so many different directions. Here is a quick recap on what has been going on at Light & Dwell as of late. Light And Dwell, California Cool, Architecture
With a number of projects, we are always being pulled in so many different directions. Here is a quick recap on what has been going on at Light & Dwell as of late. Light And Dwell, California Cool, Architecture
Check out this image
Check out this image
Chic House 4 Piece Arched Entertainment Center with Storage, Modular TV Stand for TVs up to 65", Acrylic Display Cabinets & Open Hutch Bookshelves, Black
Chic House 4 Piece Arched Entertainment Center with Storage, Modular TV Stand for TVs up to 65", Acrylic Display Cabinets & Open Hutch Bookshelves, Black

Upgrading Legacy Code

If you're maintaining legacy code that uses cmp, you should consider upgrading it. While it's not strictly necessary - Python 2 is still supported officially, and cmp is still usable there - upgrading to Python 3 has many other benefits.

Moreover, using cmp in new code could trips up other developers who might not recognize it. If you want your code to be easily understandable and maintainable, it's best to use modern, well-known techniques.

So, what do you do when you see from past.builtins import cmp in your code? You respect the past, but look towards the future. It's a reminder of where Python has been, but it's also a nudge to where it's going - and where you should be going with your code.