From the course: Python Standard Library Essential Training (2019)
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Using operator functions - Python Tutorial
From the course: Python Standard Library Essential Training (2019)
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.