From the course: Python Standard Library Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using operator functions

Using operator functions - Python Tutorial

From the course: Python Standard Library Essential Training

Start my 1-month free trial

Using operator functions

- [Man] In the previous example, we saw that when the data that you're trying to sort consists of complex objects, you need to be able to identify the field, or property value, to preform the comparison on. In that example, I accomplished that by declaring an inline Lambda function. But this is such a common scenario that Python provides specific functions for doing just this. Let's open up the sort operators underscore start file. The operator module provides the functions item getter, method caller, and adder getter. So, let's take a look at each one of these. So, here in this example I have a product class and a list of product objects similar to the previous example. So, to sort the list by product weights, I can just use the adder getter function to retrieve the weight property. So, let's go ahead and do that. And you can see at the top of the file from operator I have imported the adder getter, method caller, and item getter functions. So, what I'm going to do is print the…

Contents